JSON嵌套在POJO中 [英] JSON getting nested in a 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 $ c创建JSON对象使用HashMap从GSON获得$ c>:
$ b $ $ $ $ $ $ $ $ profile profileInJson = new JSONObject(gson.toJson(map1));
其中profileDataInJson的签名为: 现在生成的JSON如下所示: 其中,我在主要profileData对象中得到一个名为map的不需要的对象。 但是,当我在循环内打印时,我得到 Whish正是我想要的profileData对象,不嵌套 我如何解决这个问题? 我已经意识到我可以通过将D类中的profileData类型从JSONObject转换为String来实现此目的,这将导致转义字符 - 但是,我正在寻找一个通用的解决方案 编辑: map1有两种构建方式,取决于用户输入和两种方式如下所示: 签名: 令我困惑的是当我尝试遍历profileData并尝试通过名称map获取json对象时,我得到一个nullPointer例外 您不需要使用 I have a POJO class as: Now I populate this class like: I create JSON object for Where the signature of profileDataInJson is: Now the resultant JSON is like: Wherein I get an unwanted object called map inserted in my main profileData object. However when I print this inside the loop I get Whish is exactly what I want inside profileData object, without nesting the 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: And: Signature: 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 这篇关于JSON嵌套在POJO中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! JSONObject profileDataInJson = null; $
profileData:{map:{ioCinema:firstValue,ioSIMAvailable:firstKey,Name:onePair}}
{ ``ioCinema:firstValue,ioSIMAvailable:firstKey,Name:onePair}`
map
对象。
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();
Gson
来转换hashmap到一个json对象。
简单地使用:
$ b $ profileDataInJson = new JSONObject(map);
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
from GSON using a HashMap: profileDataInJson = new JSONObject(gson.toJson(map1));
JSONObject profileDataInJson = null;
"profileData":{"map":{"ioCinema":"firstValue","ioSIMAvailable":"firstKey","Name":"onePair"}}
{`"ioCinema":"firstValue","ioSIMAvailable":"firstKey","Name":"onePair"}`
map
object. 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));
}
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);
}
ProfileData profileData = new ProfileData();
Gson
to convert hashmap to a json object.
Simply use: profileDataInJson = new JSONObject(map);