如何解析JsonPrimitive

时间:2015-03-29 13:18:40

标签: json gson

我有以下需要解析的json:

{
 "resource1": {
  "fare": 7511,
  "typeoftravel": "domestic",
  "vertical": "flight",
  "extra": "[{'duration': u'3h 0m', 'arrtime': u'2015-05-05t1930', 'nostops': 0, 'deptime': u'2015-05-05t1630', 'flightno': u'6E478'}]",
  "roundtrip": "O",
  "destination": "BLR",
  "returndate": 0,
  "lastupdated": "2015-03-29T10:40:12",
  "source": "IXC",
  "carrier": "IndiGo",
  "date": 20150505,
  "class": "E"
 },
 "resource2": {
  "fare": 6320,
  "typeoftravel": "domestic",
  "vertical": "flight",
  "extra": "[{'duration': u'4h 25m', 'arrtime': u'2015-05-06t2210', 'nostops': 0, 'deptime': u'2015-05-06t1745', 'flightno': u'9W7076'}]",
  "roundtrip": "O",
  "destination": "BLR",
  "returndate": 0,
  "lastupdated": "2015-03-29T17:08:09",
  "source": "IXC",
  "carrier": "Jet Airways",
  "date": 20150506,
  "class": "E"
 }
}

我无法确定哪种字段"额外"是。它不是jsonObject或jsonArray。我能够将它转换为jsonPrimitive,但现在我如何解析它。我正在使用Gson进行解析。

1 个答案:

答案 0 :(得分:1)

假设您有以下课程:

<强>资源:

public class Resources {

    Resource resource1;
    Resource resource2;
}

<强>资源:

import java.util.List;

public class Resource {

    int fare;
    String typeoftravel;
    String vertical;
    String roundtrip;
    String destination;
    int returndate;
    String lastupdated;
    String source;
    String carrier;
    long date;
    String clazz;
    List<Extra> extra;
}

<强>附加

public class Extra {

    String duration;
    String arrtime;
    int nostops;
    String deptime;
    String flightno;
}

一种可能的解决方案是向您的JsonSerializer对象注册新的Gson

  1. 复制除extra
  2. 以外的所有常规字段
  3. extra解压缩为字符串
  4. 再次解析提取extra以删除引号
  5. 然后使用TypeToken
  6. 将其反序列化为List

    请参阅以下代码以获取解释

    <强> ResourceDeserializer

    import java.lang.reflect.Type;
    import java.util.List;
    
    import com.google.gson.JsonDeserializationContext;
    import com.google.gson.JsonDeserializer;
    import com.google.gson.JsonElement;
    import com.google.gson.JsonObject;
    import com.google.gson.JsonParseException;
    import com.google.gson.JsonParser;
    import com.google.gson.reflect.TypeToken;
    
    
    public class ResourceDeserializer implements JsonDeserializer<Resource> {
    
        @Override
        public Resource deserialize(JsonElement value, Type type,
                JsonDeserializationContext context) throws JsonParseException {
    
            final JsonObject resourceJson = value.getAsJsonObject();
    
            final Resource resource = new Resource();
            resource.fare = resourceJson.get("fare").getAsInt();
            resource.typeoftravel = resourceJson.get("typeoftravel").getAsString();
            resource.vertical = resourceJson.get("vertical").getAsString();
            resource.roundtrip = resourceJson.get("roundtrip").getAsString();
            resource.destination = resourceJson.get("destination").getAsString();
            resource.returndate = resourceJson.get("returndate").getAsInt();
            resource.lastupdated = resourceJson.get("lastupdated").getAsString();
            resource.source = resourceJson.get("source").getAsString();
            resource.carrier = resourceJson.get("carrier").getAsString();
            resource.date = resourceJson.get("date").getAsLong();
            resource.clazz = resourceJson.get("class").getAsString();
    
            Type listType = new TypeToken<List<Extra>>(){}.getType();
    
            String extraStr = resourceJson.get("extra").getAsString();
    
            extraStr = extraStr.replaceAll("u?(\'[^\']+\')", "$1");
    
            JsonElement extraList = new JsonParser().parse(extraStr);
    
            resource.extra = context.deserialize(extraList, listType);
    
            return resource;
        }
    
    }
    

    注册上述解串器

    Gson gson = new GsonBuilder()
            .registerTypeAdapter(Resource.class, new ResourceDeserializer())
            .create();
    
      

    请注意,我将属性class重命名为clazz,因为class   是一个保守的词!