如何制作JSON对象的JSON对象

时间:2015-01-30 02:17:41

标签: java android json

我一直在使用一些JSON对象并且一直将外部JSON保留为数组,但是可以保留外部JSON对象并使其包含其他JSON对象或数组吗?

这就是我所拥有的,它的形式正确且运作良好:

{
"outer":[{
  "profile":{
     "image":"",
     "name":"",
     "password":"",
     "favorites":[

     ]
  },
  "friends":[
     {
        "name":"",
        "image":"",
        "number":"",
        "type":"",
        "birthday":"",
        "state":""
     }
  ]
}]
}

但是,是否可以这样做:

{
"outer":{
  "profile":{
     "image":"",
     "name":"",
     "password":"",
     "favorites":[

     ]
  },
  "friends":[
     {
        "name":"",
        "image":"",
        "number":"",
        "type":"",
        "birthday":"",
        "state":""
     }
  ]
}
}

这也是正确的形式,但我在向Android中的单个JSON对象添加多个JSON对象和JSON数组时遇到问题。每次我有外部JSON对象时,它会覆盖我添加另一个对象时已存在的任何对象。

这是我到目前为止所得到的。 obj1是配置文件JSON对象,obj2是朋友JSON对象:

JSONObject profile = new JSONObject();
profile.put("profile", obj1);
JSONObject friends = new JSONObject();
friends.put("friends", obj2);
JSONObject outer = new JSONObject():
outer.put("outer", profile);
outer.put("outer", friends);

2 个答案:

答案 0 :(得分:0)

这是你的错误:

JSONObject profile = new JSONObject();
profile.put("profile", obj1);
JSONObject friends = new JSONObject();
friends.put("friends", obj2);
JSONObject outer = new JSONObject():
outer.put("outer", profile); 
outer.put("outer", friends); // you are overriding the profile value you have just put

使用JSONObject s:

来实现此示例
{
    "outer":
     {
        "profile":
         {
            "image":"",
             "name":"",
             "password":"",
             "favorites": [] 
         },
        "friends":
         {
             "name":"",
             "image":"",
             "number":"",
             "type":"",
             "birthday":"",
             "state":""
         } 
    }
}

你应该这样做:

JSONObject profile = new JSONObject();
profile.put("image", "");
profile.put("name", "");
profile.put("password", "");
profile.put("favorites", new JSONArray());

JSONObject friends = new JSONObject();
friends.put("name", "");
friends.put("image", "");
friends.put("number", "");
friends.put("type", "");
friends.put("byrthday", "");
friends.put("state", "");

JSONObject outer = new JSONObject():
outer.put("profile", profile);
outer.put("friends", friends);

我强烈建议您将您的String JSON解析为JAVA对象,以便将来更好地可读性更便宜的维护

为此,我建议您使用像GSON这样的外部库,并像这样使用它:

String json; // your JSON object as a string
Gson gson = new Gson(); // initializing the library object 
YourJavaObject yourJavaObject = gson.fromJson(json, YourJavaObject.class) // parsing

public class YourJavaObject 
{
    Profile profile;
    Friends friends;
}

public class Profile
{
    String image;
    String name;
    String password;
    List<Object> favorites;
}

public class Friends
{
    String name;
    String image;
    String number;
    String type;
    String birthday;
    String state;
}

答案 1 :(得分:0)

第二个JSON的主要区别在于您创建的是List<outer>而不是outer个对象。

JSONObject profile = new JSONObject();
profile.put("image", anImage); //pseudo code
profile.put("name", aProfileName); //pseudo code
//...and so on

JSONObject friends = new JSONObject();
friends.put("name", aName);
//...and so on

JSONObject outer = new JSONObject();
outer.put("profile", profile);
outer.put("friends", friends);

JSONObject outers = new JSONObject();
outers.put("outer", outer);