zoukankan      html  css  js  c++  java
  • 奇葩json结构解析--key是数字的json处理

    json结构如下:

    {
        "ret": "ok",
        "data": {
            "57230": {
                "cat_id": "57230",
                "alpha": "",
                "title": "一汽-大众奥迪",
                "malpha": "Y",
                "catpid": "57229",
                "catType": "2",
                "child": [
                    {
                        "cat_id": "57233",
                        "alpha": "A",
                        "title": "奥迪Q5",
                        "malpha": null,
                        "catpid": "57230",
                        "catType": "3",
                        "Priced": false
                    },
                    {
                        "cat_id": "126193",
                        "alpha": "A",
                        "title": "奥迪Q3",
                        "malpha": null,
                        "catpid": "57230",
                        "catType": "3",
                        "Priced": false
                    },
                    {
                        "cat_id": "126537",
                        "alpha": "A",
                        "title": "奥迪A3",
                        "malpha": null,
                        "catpid": "57230",
                        "catType": "3",
                        "Priced": false
                    },
                    {
                        "cat_id": "57231",
                        "alpha": "A",
                        "title": "奥迪A4L",
                        "malpha": null,
                        "catpid": "57230",
                        "catType": "3",
                        "Priced": false
                    },
                    {
                        "cat_id": "57232",
                        "alpha": "A",
                        "title": "奥迪A6L",
                        "malpha": null,
                        "catpid": "57230",
                        "catType": "3",
                        "Priced": true
                    }
                ]
            },
            "success": true
        }
        }

    "data"下面的数字部分是个list,结构相同,但是数字可能会发生变化,因为公司业务原因,不可能在后台统一数字部分的内容,这种结构遂蛋疼。不能直接利用gson这种直接去处理,gsonformat生成也会parse err。

    但是活得干,问题得解决,如何解决呢:

    如下就是解决方式,注意红色部位,然后就可以处理数字对应的valuse的内容,利用gson去解析:

    if ( !isNull( response ) )
    {
        try {
            JSONObject jsonObject0 = new JSONObject(
                response.trim() );
            JSONObject jsonObject = new JSONObject(
                JsonUtil.getString( jsonObject0, "data" ) );
            boolean isSuccess = JsonUtil.getBoolean( jsonObject,
                                 "success" );
            childEntities.clear();
            if ( isSuccess )
            {
                Iterator<?> keys = jsonObject.keys();
                while ( keys.hasNext() )
                {
                    String key = (String) keys.next();
                    if ( jsonObject.get( key ) instanceof JSONObject )
                    {
                        String content = jsonObject.get( key )
                                 .toString();
                        PriceManagerResult.DataEntity.ChildEntity
                            childEntity = Json.get()
                                      .toObject( content,
                                         PriceManagerResult.DataEntity.ChildEntity.class );
                        childEntities.add( childEntity );
                    }
                }
            }
            KLog.i( childEntities.size() );
            mPriceManagerAdapter2.notifyDataSetChanged();
        } catch ( JSONException e ) {
            e.printStackTrace();
        }
    }
  • 相关阅读:
    STM32F103ZET6 之 ADC+TIM+DMA+USART 综合实验
    关于Stm32定时器+ADC+DMA进行AD采样的实现
    stm32之TIM+ADC+DMA采集50HZ交流信号
    STM32F103VET6 ADC采集64点做FFT变换
    STM32f103的数电采集电路的DMA设计和使用优化程序
    Python 实现 Html 转 Markdown(支持 MathJax 数学公式)
    快速傅里叶变换FFT& 数论变换NTT
    拆系数FFT
    Python 中文编码
    Python 编写一个有道翻译的 workflow 教程
  • 原文地址:https://www.cnblogs.com/androidsuperman/p/5108857.html
Copyright © 2011-2022 走看看