0

我得到一个服务器响应,其中包含以下列方式构建的复杂对象:

json array of object type A with
           { jsonobject with a json array of object type B
                 }

我正在尝试将其反序列化为我的对象 typeA 和 object typeB ,如下例所示:

public class ObjectA{
   String a;
   int b;
   ArrayList<ObjectB> list;
}

public class ObjectB{
   String a1;
   int b2;
   String c3;
}

这是我的 JSON 示例

[
   {
      "a": "a",
      "b": 1,
      "list": [
         {
            "a1": "a1",
            "b2": 2,
            "c3": "c3"
         },
         {
            "a1": "a1",
            "b2": 2,
            "c3": "c3"
         }
      ]
   },
   {
      "a": "a",
      "b": 1,
      "list": [
         {
            "a1": "a1",
            "b2": 2,
            "c3": "c3"
         },
         {
            "a1": "a1",
            "b2": 2,
            "c3": "c3"
         }
      ]
   }
]

我如何反序列化这个?

4

3 回答 3

2

由于您已经有了一个类层次结构,您可以利用它并让 Gson 为您完成所有工作。为了更好地解释我的意思,我准备了一个可以运行的代码:

package stackoverflow.questions.q18123430;

import java.lang.reflect.Type;
import java.util.*;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class Q18123430 {

    public static class ObjectA {
        String a;
        int b;
        ArrayList<ObjectB> list;

        @Override
        public String toString() {
            return "ObjectA [a=" + a + ", b=" + b + ", list=" + list + "]";
        }

    }

    public static class ObjectB {
        String a1;
        int b2;
        String c3;

        @Override
        public String toString() {
            return "ObjectB [a1=" + a1 + ", b2=" + b2 + ", c3=" + c3 + "]";
        }

    }

    public static void main(String[] args) {
        String json = "[{  a:\"a\",b:1,list:  [{a1:\"a1\",b2:2,c3:\"c3\"},{a1:\"a1\",b2:2,c3:\"c3\"}]}, {  a:\"a\",b:1,list:  [{a1:\"a1\",b2:2,c3:\"c3\"},{a1:\"a1\",b2:2,c3:\"c3\"}]}]";
        Type listOfObjectA = new TypeToken<List<ObjectA>>() {
        }.getType();

        Gson g = new Gson();
        ArrayList<ObjectA> result = g.fromJson(json, listOfObjectA);
        System.out.println(result);

    }

}

这是我的执行结果:

[ObjectA [a=a, b=1, list=[ObjectB [a1=a1, b2=2, c3=c3], ObjectB [a1=a1, b2=2, c3=c3]]], ObjectA [a=a, b=1, list=[ObjectB [a1=a1, b2=2, c3=c3], ObjectB [a1=a1, b2=2, c3=c3]]]]
于 2013-11-13T23:11:09.537 回答
1
JSONArray jsArray = new JSONArray(); //This is your outer most JSONARRAY
for(int i=0;i<jsArray.length;i++)
{
     JSONObject innerJsonObj = jsArray.getJSONObject(i);//this is your inner jsonobject
     Log.v("a",innserJsonObj.getString("a"));
     Log.v("b",innserJsonObj.getString("b"));
     Log.v("list",innserJsonObj.getString("list")); // you have to use another loop to deal with this json array
     //to generate json array you can use:
     JSONArray innerJsonArray = new JSONArray(innserJsonObj.getString("list"));
}
于 2013-08-08T10:50:19.977 回答
1

您可以在一行中使用 Gson 库在 Java 中反序列化,如下所示

String jsonInputStr = "your json string";

Gson gson = new Gson();
List<ObjectA> objectList = gson.fromJson(jsonInputStr, new TypeToken<List<ObjectA>>(){}.getType());
于 2014-01-30T11:49:58.653 回答