Gson解析比较复杂的json数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Gson解析比较复杂的json数据相关的知识,希望对你有一定的参考价值。

就是这种接口传来的数据,数据太杂了,如何选取几个有用的数据单个解析出来??用Gson。
麻烦大牛点一下,给下代码更好,感谢。

"showapi_res_code": 0,
"showapi_res_error": "",
"showapi_res_body":
"cityInfo":
"c2": "lanzhou",
"c3": "兰州",
,
"f1":
"day": "20150906",
"day_air_temperature": "31",
"day_weather": "晴",
"day_weather_pic":
"night_air_temperature": "19",
"night_weather": "多云",
,

参考技术A 你先定义类,然后使用Gson的fromJson方法转换成类。
下面是我的代码:
public class showapi_res_body
private cityInfo cityInfo = new cityInfo();

private f1 f1 = new f1();

@Override
public String toString()
return "showapi_res_body [cityInfo=" + cityInfo.toString() + ", f1=" + f1.toString() + "]";




public class day_weather_pic
private int night_air_temperature = 19;
private String night_weather = "多云";
@Override
public String toString()
return "day_weather_pic [night_air_temperature=" + night_air_temperature + ", night_weather=" + night_weather
+ "]";




public class f1
private String day="20150906";
private int day_air_temperature=3;
private String day_weather= "晴";
private day_weather_pic day_weather_pic = new day_weather_pic();
@Override
public String toString()
return "f1 [day=" + day + ", day_air_temperature=" + day_air_temperature + ", day_weather=" + day_weather
+ ", day_weather_pic=" + day_weather_pic.toString() + "]";




public class cityInfo

private String c2 ="lanzhou";
private String c3 ="兰州";
@Override
public String toString()
return "cityInfo [c2=" + c2 + ", c3=" + c3 + "]";




public class showapi
private int showapi_res_code = 0;
private String showapi_res_error = "";
private showapi_res_body showapi_res_body = new showapi_res_body();
@Override
public String toString()
return "showapi [showapi_res_code=" + showapi_res_code + ", showapi_res_error=" + showapi_res_error
+ ", showapi_res_body=" + showapi_res_body.toString() + "]";




public class test
public static void main(String[] args)
showapi anObject = new showapi();

Gson gson = new Gson();
String json = gson.toJson(anObject);
System.out.println(json.toString());

showapi a = gson.fromJson(json, showapi.class);

System.out.println(a.toString());



运行后输出的日志:

"showapi_res_code":0,"showapi_res_error":"","showapi_res_body":"cityInfo":"c2":"lanzhou","c3":"兰州","f1":"day":"20150906","day_air_temperature":3,"day_weather":"晴","day_weather_pic":"night_air_temperature":19,"night_weather":"多云"

showapi [showapi_res_code=0, showapi_res_error=, showapi_res_body=showapi_res_body [cityInfo=cityInfo [c2=lanzhou, c3=兰州], f1=f1 [day=20150906, day_air_temperature=3, day_weather=晴, day_weather_pic=day_weather_pic [night_air_temperature=19, night_weather=多云]]]]本回答被提问者采纳

关于解析各种复杂json

参考技术A def dict_generator(indict, pre=None):

    pre = pre[:] if pre else []

    if isinstance(indict, dict):

        for key, value in indict.items():

            if isinstance(value, dict):

                if len(value) == 0:

                    yield pre + [key, '']

                else:

                    for d in dict_generator(value, pre + [key]):

                        yield d

            elif isinstance(value, list):

                if len(value) == 0:

                    yield pre + [key, '[]']

                else:

                    for v in value:

                        for d in dict_generator(v, pre + [key]):

                            yield d

            elif isinstance(value, tuple):

                if len(value) == 0:

                    yield pre + [key, '()']

                else:

                    for v in value:

                        for d in dict_generator(v, pre + [key]):

                            yield d

            else:

                yield pre + [key, value]

    else:

        yield indict

def print_keyvalue_by_key(input_json, key):

    key_value = ""

    if isinstance(input_json, dict):

        for json_result in input_json.values():

            if key in input_json.keys():

                key_value = input_json.get(key)

            else:

                print_keyvalue_by_key(json_result, key)

    elif isinstance(input_json, list):

        for json_array in input_json:

            print_keyvalue_by_key(json_array, key)

    if key_value != "":

        for value in key_value:

            if "contents" == key:

                s = value[:-1].lstrip("[")

                json_loads = json.loads(s)

                json_dumps = json.dumps(json_loads)

                return json_dumps

            else:

                list_add.append(value)

        return list_add

def get_all_field(data,key,params_key):

"""递归解析json

    :param data: json数据

    :return: key的值和params_key的值"""

        if isinstance(data,dict):

            for k, vin data.items():

                if key == kand params_key in data:

                    yield v,data[params_key]

                elif k !=params_key:

                    for a, bin get_all_field(v,key,params_key):

                        yield a, b    

        elif isinstance(data,list):

            for v in data:

                for a, b in get_all_field(v,key,params_key):

                    yield a, b

if __name__ =='__main__':

        for a,b in  get_all_field(json, filed,param):

                print(a,b)    

以上是关于Gson解析比较复杂的json数据的主要内容,如果未能解决你的问题,请参考以下文章

android使用gson解析嵌套复杂的json数据,数据怎么显示到布局上,布局怎么写

安卓怎么用gson解析服务端返回的复杂json数据

求教json的有关问题,google库gson的bug有关问题

java怎么取json数据的值

java怎么使用gson解析json字符串

json解析性能比较(gson与jackson) (zz)