没有键的json数组的gson模型 [英] gson model for json array without key

查看:176
本文介绍了没有键的json数组的gson模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的客户有以下json:

  {
id:1234,
delivery_date:1234567890,

actions:[
[foo,true],
[bar,true]
],
customer:{
id:12345,
company:,
firstname:John,
lastname: Smith,
action:[dothis,true]
},
childs:[123abc2132312312,11232432943493]
}

我想将actions数组解析为List<动作>动作列表和作为动作动作的单个动作。

随着

  class Action {
String action;
布尔型yesno;
}

子元素Array as List< Child> childs with

  class Child {
String id
}

没有json键可以吗?

解决方案

我用自定义的反序列化器解决了我的问题。感谢dzsonni的提示。



在Gson根类中:

  private ArrayList< Action> parcel_actions = new ArrayList< Action>(); 

动作类

  class Action {
String action;
布尔型yesno;

$ / code>

解串器:

  public class ActionDeserializer实现JsonDeserializer< ArrayList< Action>> {

@Override
public ArrayList< Action>反序列化(JsonElement json,类型typeOfT,JsonDeserializationContext上下文)抛出JsonParseException {
ArrayList< Actions> list = new ArrayList< Action>(){}; $()。getAsSson(); get(0).isJsonPrimitive()){
String action = json.getAsJsonArray()。get(0).getAsString();

if(json.getAsJsonArray
boolean doIt = json.getAsJsonArray()。get(1).getAsBoolean();
list.add(new Action(action,doIt)); $ J

$ {
for(JsonElement element:json.getAsJsonArray()){
String action = element.getAsJsonArray()。get(0).getAsString();
boolean doIt = element.getAsJsonArray()。get(1).getAsBoolean();
list.add(new Action(action,doIt));
}
}

返回列表;


然后将它添加到你的gson中

  GsonBuilder builder = new GsonBuilder(); 
builder.registerTypeAdapter(new TypeToken< ArrayList< Action>>(){}。getType(),new ActionsDeserializer());
Gson gson = builder.create();


I have the following json from our customer:

{
    "id": 1234,
    "delivery_date":  1234567890,

    "actions": [
        [ "foo", true],
        [ "bar", true]
    ],
    "customer":  {
        "id": 12345,
        "company": "",
        "firstname": "John",
        "lastname": "Smith",
        "action": ["dothis", true]
    },
    "childs": [ 123abc2132312312,11232432943493]
}

I want to parse the "actions" array as a List< Actions> actionList and the single "action" as Action action.

With

class Action {
  String action;
  boolean yesno;
}

And the childs Array as List< Child> childs with

class Child{
  String id
}

Is that possible without the json keys?

解决方案

I solved it my self with a custom deserializer. Thanks to dzsonni for the hint.

In the Gson root class:

private ArrayList<Action> parcel_actions = new ArrayList<Action>();

the action class

class Action {
  String action;
  boolean yesno;
}

the deserializer:

public class ActionDeserializer implements JsonDeserializer<ArrayList<Action>> {

    @Override
    public ArrayList<Action> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        ArrayList<Actions> list = new ArrayList<Action>(){};

        if(json.getAsJsonArray().get(0).isJsonPrimitive()){
            String action = json.getAsJsonArray().get(0).getAsString();
            boolean doIt = json.getAsJsonArray().get(1).getAsBoolean();
            list.add(new Action(action, doIt));
        }
        else {
            for(JsonElement element : json.getAsJsonArray()) {
                String action = element.getAsJsonArray().get(0).getAsString();
                boolean doIt = element.getAsJsonArray().get(1).getAsBoolean();
                list.add(new Action(action, doIt));
            }
        }

        return list;
    }
}

then just add it to your gson

GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(new TypeToken<ArrayList<Action>>(){}.getType(), new ActionsDeserializer()); 
Gson gson = builder.create();

这篇关于没有键的json数组的gson模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆