如何使用模型类解析此Json格式化数据

时间:2017-08-03 06:49:26

标签: android json

我能够以正常方式解析Json Array下面的这个。但现在我想使用Model Classes解析这个String。我怎样才能做到这一点。

    [
        {
            "Id": "-1",
            "Name": "name1",

            "Children": [
 {
                    "Id": "2",
                    "Name": "name2",
                    "Children": [
                        {
                            "Id": "3",
                            "Name": "name3",
                            "Children": [
                                {
                                    "Id": "15",
                                    "Name": "name222",
                                    "Children": [
                                        {
                                            "Id": "16",
                                            "Name": "xddxxcc",
                                        },
                                        {
                                            "Id": "20",
                                            "Name": "sdd",

                                            "Children": []
                                        },
                                        {
                                            "Id": "23",
                                            "Name": "cdaca",

                                            "Children": []
                                        },
                                        {
                                            "Id": "28",
                                            "Name": "assa",
                                            "Children": [ ]
                                        }
                                    ]
                                },

                        {
                            "Id": "15",
                            "Name": "fdcds"
                            "Children": []
                        }]}]}]

我已经使用了Model类,如下所示。

class Model1{
String id;
String Name;
ArrayList<Children>Children=new ArrayList<>();
//Getters and setters
}

和Children.class文件是

class Children{
 String id;
    String Name;
    ArrayList<Children>Children=new ArrayList<>();
    //Getters and setters
}

请有人帮助我。在此先感谢

1 个答案:

答案 0 :(得分:2)

您的JSON无效。正确如下:

[  
   {  
      "Id":"-1",
      "Name":"name1",
      "Children":[  
         {  
            "Id":"2",
            "Name":"name2",
            "Children":[  
               {  
                  "Id":"3",
                  "Name":"name3",
                  "Children":[  
                     {  
                        "Id":"15",
                        "Name":"name222",
                        "Children":[  
                           {  
                              "Id":"16",
                              "Name":"xddxxcc",
                              "Children":[  

                              ]
                           },
                           {  
                              "Id":"20",
                              "Name":"sdd",
                              "Children":[  

                              ]
                           },
                           {  
                              "Id":"23",
                              "Name":"cdaca",
                              "Children":[  

                              ]
                           },
                           {  
                              "Id":"28",
                              "Name":"assa",
                              "Children":[  

                              ]
                           }
                        ]
                     },
                     {  
                        "Id":"15",
                        "Name":"fdcds",
                        "Children":[  

                        ]
                     }
                  ]
               }
            ]
         }
      ]
   }
]

这是你的模特:

public class Child
{
    public string Id { get; set; }
    public string Name { get; set; }
    public Child[] Children { get; set; }
}

为了将json解析为你的模型,请使用:(我假设你正在使用java)(reference

<强>杰克逊

import com.fasterxml.jackson.databind.ObjectMapper;
ObjectMapper mapper = new ObjectMapper();
Child[] myObjects = mapper.readValue(json, Child[].class)

<强> GSON

Gson gson = new Gson();
Child[] myObjects = gson.fromJson(json, Child[].class);