JSON嵌套在POJO中 [英] JSON getting nested in a POJO

查看:95
本文介绍了JSON嵌套在POJO中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个POJO类:

  public class D {

private JSONObject profileData;


public JSONObject getProfileData()
{
return profileData;
}

public void setProfileData(JSONObject profileData)
{
this.profileData = profileData;
}

}

现在我将这个类填充为:

  for(int i = 0; i< identities.size(); i ++){
D d = new d();

d.setProfileData(profileData);

dList.add(d);
}

我为 profileData
$ b $ $ $ $ $ $ $ $ profile profileInJson = new JSONObject(gson.toJson(map1));

其中profileDataInJson的签名为: JSONObject profileDataInJson = null;

现在生成的JSON如下所示:

  profileData:{map:{ioCinema:firstValue,ioSIMAvailable:firstKey,Name:onePair}} 

其中,我在主要profileData对象中得到一个名为map的不需要的对象。



但是,当我在循环内打印时,我得到

  { ``ioCinema:firstValue,ioSIMAvailable:firstKey,Name:onePair}`

Whish正是我想要的profileData对象,不嵌套 map 对象。

我如何解决这个问题?

我已经意识到我可以通过将D类中的profileData类型从JSONObject转换为String来实现此目的,这将导致转义字符 - 但是,我正在寻找一个通用的解决方案



编辑:

map1有两种构建方式,取决于用户输入和两种方式如下所示:

  if(args.length> = 4&& args [1] .equalsIgnoreCase(onePair )){

map1 = new HashMap<>();
String key1 = args [2];
字符串value1 = args [3];


map1.put(key1,value1);


profileDataInJson = new JSONObject(gson.toJson(map1));

$ b $ / code $ / pre
$ b $
$ b如果(args.length> = 1&& args [0] .equalsIgnoreCase(update)){

if($ b pre> args.length> = 2)
profileData.setName(args [1]!= null?args [1]:);

if(args.length> = 3)
profileData.setSIMAvailable(args [2]!= null?args [2]:);
profileDataInJson = new JSONObject(profileData);

签名: ProfileData profileData = new ProfileData();



令我困惑的是当我尝试遍历profileData并尝试通过名称map获取json对象时,我得到一个nullPointer例外

解决方案

您不需要使用 Gson 来转换hashmap到一个json对象。
简单地使用:
$ b $ profileDataInJson = new JSONObject(map);


I have a POJO class as:

public class D{

    private JSONObject profileData;


    public JSONObject getProfileData ()
    {
        return profileData;
    }

    public void setProfileData (JSONObject profileData)
    {
        this.profileData = profileData;
    }

}

Now I populate this class like:

for (int i =0; i<identities.size();i++){
            D d = new D();

            d.setProfileData(profileData);

            dList.add(d);
        }

I create JSON object for profileData from GSON using a HashMap:

profileDataInJson = new JSONObject(gson.toJson(map1));

Where the signature of profileDataInJson is: JSONObject profileDataInJson = null;

Now the resultant JSON is like:

"profileData":{"map":{"ioCinema":"firstValue","ioSIMAvailable":"firstKey","Name":"onePair"}}

Wherein I get an unwanted object called map inserted in my main profileData object.

However when I print this inside the loop I get

{`"ioCinema":"firstValue","ioSIMAvailable":"firstKey","Name":"onePair"}`

Whish is exactly what I want inside profileData object, without nesting the map object.

How do I solve this?

"I am already aware that I can achieve this by converting the type of profileData in D class from JSONObject to String, which will induce escape characters - However I am looking for a generic solution"

EDIT:

map1 is constructed in two ways, depending on user input and both ways are as follows:

if (args.length >= 4 && args[1].equalsIgnoreCase("onePair")) {

                    map1 = new HashMap<>();
                    String key1 = args[2];
                    String value1 = args[3];


                    map1.put(key1, value1);


                    profileDataInJson = new JSONObject(gson.toJson(map1));

                }

And:

if (args.length >= 1 && args[0].equalsIgnoreCase("update")) {

                if (args.length >= 2)
                    profileData.setName(args[1] != null ? args[1] : "");

                if (args.length >= 3)
                    profileData.setSIMAvailable(args[2] != null ? args[2] : "");
                  profileDataInJson = new JSONObject(profileData);
}

Signature: ProfileData profileData = new ProfileData();

The thing which puzzles me is when I try to traverse profileData and try to fetch the json object by name "map" I get a nullPointer exception

解决方案

You don't need to use Gson to convert hashmap to a json object. Simply use:

profileDataInJson = new JSONObject(map);

这篇关于JSON嵌套在POJO中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆