Android将gson解析的json数组转换为自定义对象

时间:2016-05-20 13:28:03

标签: android json object arraylist gson

我有一个名为Img和Order的自定义对象:

public class Order {
private String name;
private String count;

public Order(String name, String count) {
    this.name = name;
    this.count = count;
}


public String getName() {
    return name;
}


public String getCount(){
    return count;
}
}

public class Img {
private String url;
private String name;
private String number;
private String id;

public Img(String url, String id, String name, String number) {
    this.url = url;
    this.id = id;
    this.name = name;
    this.number = number;
}


public String getUrl() {
    if (url.contains("http://") || url.contains("https://")) {
        return url;
    } else {
        return "http://sampleurl.com/" + url;
    }
}

public String getName() {
    return name;
}

public String getNumber(){
    return number;
}

public String getId(){
    return id;
}
}

以下是来自服务器的json响应:

订单:

{
"items": [
 {
 "id": {
 "kind": "Order",
 "appId": "s-contacts",
 "id": "5629499534213120",
 "parent": {
  "kind": "Contact",
  "appId": "s~contacts",
  "id": "4853444577853440",
  "complete": true
 },
 "complete": true
 },
 "name": "LCD",
 "count": 3,
 "kind": "orderendpoint#resourcesItem"
},
{
 "id": {
  "kind": "Order",
  "appId": "scontacts",
  "id": "5066549580791808",
  "parent": {
   "kind": "Contact",
   "appId": "s-contacts",
   "id": "4853444577853440",
   "complete": true
  },
  "complete": true
 },
 "name": "LCD",
 "count": 10,
 "kind": "orderendpoint#resourcesItem"
}
],
"kind": "orderendpoint#resources",
"etag": "\"oi1IHjqo\""
}

和IMG:

 {
 "items": [
 {
 "id": "4853444577853440",
  "name": "aaaasssdddd",
  "phone": "45454545",
  "kind": "contactendpoint#resourcesItem"
 },
 {
 "id": "4919896311857152",
 "name": "Novy Kon",
 "phone": "522396",
 "kind": "contactendpoint#resourcesItem"
}

我尝试使用此代码进行IMG:

Type listType = new TypeToken<List<Img>>() {}.getType();
List<Img> records = new Gson().fromJson(json.getJSONArray("items").toString(), listType);

我是Gson的新手,我无法让这个工作,请你帮我把它放在一个清单中吗?

1 个答案:

答案 0 :(得分:0)

IMG的JSON无效。我冒昧地解决了这个问题:

{
  "items":[
    {
      "id":"4853444577853440",
      "name":"aaaasssdddd",
      "phone":"45454545",
      "kind":"contactendpoint#resourcesItem"
    },
    {
      "id":"4919896311857152",
      "name":"Novy Kon",
      "phone":"522396",
      "kind":"contactendpoint#resourcesItem"
    }
  ]
}

Img提供的自定义对象与您的JSON不符。我已使用jsonschema2pojo

从'固定'Img class重新生成JSON
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Img {

  @SerializedName("id")
  @Expose
  private String id;
  @SerializedName("name")
  @Expose
  private String name;
  @SerializedName("phone")
  @Expose
  private String phone;
  @SerializedName("kind")
  @Expose
  private String kind;

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getPhone() {
    return phone;
  }

  public void setPhone(String phone) {
    this.phone = phone;
  }

  public String getKind() {
    return kind;
  }

  public void setKind(String kind) {
    this.kind = kind;
  }
}

要将String json解析为List<Img>,请使用以下parseJson代码:

String json = "{\n" +
        "  \"items\":[\n" +
        "    {\n" +
        "      \"id\":\"4853444577853440\",\n" +
        "      \"name\":\"aaaasssdddd\",\n" +
        "      \"phone\":\"45454545\",\n" +
        "      \"kind\":\"contactendpoint#resourcesItem\"\n" +
        "    },\n" +
        "    {\n" +
        "      \"id\":\"4919896311857152\",\n" +
        "      \"name\":\"Novy Kon\",\n" +
        "      \"phone\":\"522396\",\n" +
        "      \"kind\":\"contactendpoint#resourcesItem\"\n" +
        "    }\n" +
        "  ]\n" +
        "}";

private List<Img> parseJson(String json) throws JSONException {
    json = new JSONObject(json).getJSONArray("items").toString();
    Img[] data = new Gson().fromJson(json, Img[].class);
    return Arrays.asList(data);
}