使用Gson反序列化JSON

时间:2013-08-08 10:16:40

标签: java json deserialization gson

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

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

我正在尝试将其反序列化为我的对象类型A和对象类型B,如下例所示:

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"
         }
      ]
   }
]

如何反序列化?

3 个答案:

答案 0 :(得分: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]]]]

答案 1 :(得分: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"));
}

答案 2 :(得分:1)

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

String jsonInputStr = "your json string";

Gson gson = new Gson();
List<ObjectA> objectList = gson.fromJson(jsonInputStr, new TypeToken<List<ObjectA>>(){}.getType());