Json对象多数组Parse,改造

Posted

技术标签:

【中文标题】Json对象多数组Parse,改造【英文标题】:Json object multiple array Parse, retrofit 【发布时间】:2017-09-02 10:47:27 【问题描述】:

我有一个 Json 字符串。我无法使用带有两个列表(类别和产品)的改造来响应。我怎么打电话,回拨?我应该用什么?通常不是一个列表,对吧?


"Categories": ["name":"x products",
                "products":[]
                "name":"y products",
                "products":[]
                "name":"z products",
                "products":[] ],
"CoverPageURL":"xx.png",
"Location": "Canada",
"adress":"opposite of hospital"
"ProductItems": [ 
          "large":"xx"
            "mall":"tt"
           "large":"yy"
            "mall":"gg"]
"Status": 
"ErrorCode": 0,
"Error": null,
"HasError": false
  
   

通常我会这样做

Call<Merchant> call = ToolApi.getApi().GetMerchant(BaseService.TOKEN);
        call.enqueue(new Callback<Merchant>() 
            @Override
            public void onResponse(Response<Merchant> response, Retrofit retrofit) 
                if (response.body() == null) 
else

//I bought the bottoms from another class just For example
//normaly one list and its' items. but in that case i have two list on JSON
//start
   HomePageContent homePageContent = response.body();
                    Integer errorCode = homePageContent.getStatus().getErrorCode();

                    if (errorCode == 0) 
/// What i should write here 
                        List<ItemHomePagecontent> items = homePageContent.getItems();
                        HeadRecyclerListHomePageContent.setHasFixedSize(true);
                        homePageAdapter = new HomePageAdapter(homePageContent, items, context());
                        HeadRecyclerListHomePageContent.setLayoutManager(new LinearLayoutManager(context(), LinearLayoutManager.VERTICAL, false));
                        HeadRecyclerListHomePageContent.setAdapter(homePageAdapter);
                        
                       //finish

 @Override
            public void onFailure(Throwable t) 

            
....

【问题讨论】:

你想知道如何将 JSON 解析成 POJO 吗? @AishwaryaTiwari 我编辑问题。不,不是 POJO。我想知道我如何列出清单?谢谢 您的 JSON 无效。检查您的 JSON 在 JSONLint 上是否有效。发布一个有效的 JSON,然后我们可以提供适当的帮助。 【参考方案1】:

// What i should write here 块中应该是这样的: Merchant merchant = response.body();

Merchant 类中应该是GetMerchant 返回的字段。

顺便说一句,您的 json 无效。

【讨论】:

不,我编辑问题。我想知道,如何放入 json 列表中?因为我在 json 上有 2 个列表(类别和 ProductItems) 您应该使用您的两个列表和来自 json 结构的其他字段创建类,然后创建此类的实例并填充字段。当您可以像使用 GetMerchant 请求一样通过改造发送此对象时@Jenni

以上是关于Json对象多数组Parse,改造的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Android 的改造请求中传递 JSON 对象并检索 JSON 对象?

使用数组改造解析 json 对象

如何在改造中使用原始 json 对象调用改造 @DELETE 方法?

JSON对象的解析,JSON.stringify()/JSON.parse()的用法

JS-补充-json,符号优先级

JSON.parse()和JSON.stringify()方法学习