【问题标题】:Gson can not parse nested string objectGson 无法解析嵌套的字符串对象
【发布时间】:2016-10-01 12:35:57
【问题描述】:

如何解析嵌套对象

 [{
       "id": 100500,
       "products": "[{\"id\":45,\"title\":\"All Product\"]"
    }]

模型类

public class Store {
      public  long id;
      public  List<Product> products;

}

【问题讨论】:

  • 我用替换字符串解决

标签: java android arrays json gson


【解决方案1】:

你好,把 Gson 改成

{
  "Id": "100500",
  "products": [

      {
        "id": 1,
        "title": "title 1"
      },
      {
         "id": 2,
         "title": "title 2"
      },
      {
        "id": 3,
          "title": "title 3"
      } 

  ]
}

【讨论】:

    【解决方案2】:

    不会解析

    public class Store {
          public  long id;
          public  List<Product> products;
    
    }
    
    Gson mGson= new Gson();
    storeList=mGson.fromJson(json, Store.class);
    storeList.getStores().get(0).getProducts().get(1);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-27
      相关资源
      最近更新 更多