【问题标题】:Creating a nested JSON object创建嵌套 JSON 对象
【发布时间】:2011-09-12 05:08:50
【问题描述】:

我正在尝试像这样创建一个复杂的嵌套对象:

{
   "user":{
      "type":"facebook",
      "first_name":"harsha",
      "last_name":"mv",
      "gender":"male"
   },
   "friends":[
      {
         "id":"23",
         "name":"Vachana"
      },
      {
         "id":"23",
         "name":"Jyosna"
      },
      {
         "id":"23",
         "name":"Avinash"
      }
   ]
}

Android Java 代码:

    JSONObject user = new JSONObject();
    try {
        user.put("first_name", "harsha");
        user.put("last_name", "mv");
        user.put("gender", "mail");

        System.out.println(user);
    } catch (JSONException e1) {
        e1.printStackTrace();
    }

我想嵌套其他对象,如上所示。有人能告诉我我该怎么做吗?我需要使用什么功能或格式?

【问题讨论】:

    标签: android json object


    【解决方案1】:

    'friends' 是一个JSONArray,它的每个项目都是一个JSONObject。只需使用 put 和 JSONObjectJSONArray 作为值。对于数组,您还必须指定索引。

    【讨论】:

      【解决方案2】:

      JSONObject中有一个函数

      put(String string, Object value)
      

      接受一个 JSONArray,您可以将其设为 JSONObject 的数组。我想你的“朋友”类可以生成放入列表中的 JSONObject。

      【讨论】:

        【解决方案3】:

        查看这些 JSON 编码示例,以便您轻松管理它。

        JSON Encoding

        【讨论】:

          【解决方案4】:

          我正在切换主题,但坚持您使用 Gson,因为它使用运行时 getter/setter 维护一个类中的所有元素

          【讨论】:

            【解决方案5】:

            使用这个 Json jar 文件,它会根据标记名自动设置并获取值(例如:名字、姓氏等) jar文件的下载和使用从belo链接获取

            Android json jar file

            【讨论】:

              【解决方案6】:

              Gson 最适合这种情况,使用 Gson,您可以使用常规的 java 类并将它们转换为 JSON 对象,这是一个示例:

              public class Member
              {    
              public String  ID;
              public String FirstName;    
              public String LastName;    
              public String Email;    
              public String DisplayName ;  
              public String DOB;    
              public String AboutMe;   
              public String MemberType;   
              public String UserTypeName;
              
              }
              
              initiate Member class and fill in the values
              Member member = new Member();
               member.ID = "1";
               .......
              
               //now use GSON to get the JSON
              
               Gson gson = new Gson();
               String JSONString = gson.toJson(member);
              

              更多信息请查看http://code.google.com/p/google-gson/

              【讨论】:

              • 这是怎么嵌套的?
              【解决方案7】:

              如果你想制作嵌套的 json 对象,那么你可以使用以下代码:

                          JSONObject objFirst = new JSONObject();
                          objFirst.put("description", "ADBCD");
                          objFirst.put("type", "TEXT");
              
              
                          JSONObject objSecond = new JSONObject();
                          objSecond.put("askjdhasjk", "XYZ");
              
                          objFirst.accumulate("This Nw", objSecond);
              

              【讨论】:

                【解决方案8】:
                //Import the following
                //import java.util.ArrayList;
                //import org.bson.Document;
                
                Document root= new Document();
                Document rootUser = new Document();
                ArrayList rootFriends= new ArrayList();
                Document rootFriends0 = new Document();
                Document rootFriends1 = new Document();
                Document rootFriends2 = new Document();
                
                
                
                
                rootUser.append("type","facebook");   
                rootUser.append("first_name","harsha");
                rootUser.append("last_name","mv");
                rootUser.append("gender","male");
                rootFriends0.append("id","23");
                rootFriends0.append("name","Vachana");
                rootFriends1.append("id","23");
                rootFriends1.append("name","Jyosna");
                rootFriends2.append("id","23");
                rootFriends2.append("name","Avinash");
                
                
                
                
                if (!rootUser.isEmpty()){
                root.append("user",rootUser);
                }
                if (!rootFriends.isEmpty()){
                root.append("friends",rootFriends);
                }
                if (!rootFriends0.isEmpty()){
                rootFriends.add(rootFriends0);
                }
                if (!rootFriends.isEmpty()){
                root.append("friends",rootFriends);
                }
                if (!rootFriends1.isEmpty()){
                rootFriends.add(rootFriends1);
                }
                if (!rootFriends.isEmpty()){
                root.append("friends",rootFriends);
                }
                if (!rootFriends2.isEmpty()){
                rootFriends.add(rootFriends2);
                }
                if (!rootFriends.isEmpty()){
                root.append("friends",rootFriends);
                }
                
                
                System.out.println(root.toJson());
                

                【讨论】:

                  猜你喜欢
                  • 2017-02-14
                  • 2017-12-22
                  • 1970-01-01
                  • 1970-01-01
                  • 2021-10-02
                  • 1970-01-01
                  • 1970-01-01
                  • 2013-03-26
                  • 2014-05-15
                  相关资源
                  最近更新 更多