如何使用 JsonReader 跳过不需要的值?
Posted
技术标签:
【中文标题】如何使用 JsonReader 跳过不需要的值?【英文标题】:How can I skip unwanted values using JsonReader? 【发布时间】:2013-03-20 16:36:29 【问题描述】:我试图用JsonReader
解析一个 JSON 文件。我使用skipvalue()
函数转到下一个键,但解析器没有转到下一个键,而是跳转到文件末尾。
文件如下所示:
"data":
"current_condition":[
"cloudcover":"100",
"humidity":"74",
"observation_time":"01:28 PM",
"precipMM":"0.4",
"pressure":"1005",
"temp_C":"-3",
"temp_F":"27",
"visibility":"6",
"weatherCode":"326",
"weatherDesc":[
"value":"Light snow"
],
"weatherIconUrl":[
"value":"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0011_light_snow_showers.png"
],
"winddir16Point":"NE",
"winddirDegree":"50",
"windspeedKmph":"15",
"windspeedMiles":"9"
],
"request":[
"query":"Minsk, Belarus",
"type":"City"
],
"weather":[
"date":"2013-03-20",
"precipMM":"4.4",
"tempMaxC":"-4",
"tempMaxF":"25",
"tempMinC":"-13",
"tempMinF":"10",
"weatherCode":"326",
"weatherDesc":[
"value":"Light snow"
],
"weatherIconUrl":[
"value":"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0011_light_snow_showers.png"
],
"winddir16Point":"NE",
"winddirDegree":"40",
"winddirection":"NE",
"windspeedKmph":"17",
"windspeedMiles":"11"
,
"date":"2013-03-21",
"precipMM":"0.6",
"tempMaxC":"-5",
"tempMaxF":"23",
"tempMinC":"-15",
"tempMinF":"5",
"weatherCode":"326",
"weatherDesc":[
"value":"Light snow"
],
"weatherIconUrl":[
"value":"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0011_light_snow_showers.png"
],
"winddir16Point":"NE",
"winddirDegree":"38",
"winddirection":"NE",
"windspeedKmph":"12",
"windspeedMiles":"7"
,
"date":"2013-03-22",
"precipMM":"0.1",
"tempMaxC":"-9",
"tempMaxF":"17",
"tempMinC":"-19",
"tempMinF":"-2",
"weatherCode":"119",
"weatherDesc":[
"value":"Cloudy"
],
"weatherIconUrl":[
"value":"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0003_white_cloud.png"
],
"winddir16Point":"NE",
"winddirDegree":"51",
"winddirection":"NE",
"windspeedKmph":"19",
"windspeedMiles":"12"
,
"date":"2013-03-23",
"precipMM":"0.0",
"tempMaxC":"-8",
"tempMaxF":"18",
"tempMinC":"-13",
"tempMinF":"8",
"weatherCode":"116",
"weatherDesc":[
"value":"Partly Cloudy"
],
"weatherIconUrl":[
"value":"http:\/\/www.worldweatheronline.com\/images\/wsymbols01_png_64\/wsymbol_0002_sunny_intervals.png"
],
"winddir16Point":"NNE",
"winddirDegree":"12",
"winddirection":"NNE",
"windspeedKmph":"16",
"windspeedMiles":"10"
]
这是我的代码:
String json_url = "http://free.worldweatheronline.com/feed/weather.ashx?q="+city+"&format=json&num_of_days="+Integer.toString(days)+"&key=0592d3cc1b151105131103";
JsonReader forecastJsonReader = new JsonReader(new InputStreamReader(getUrlData(json_url)));
forecastJsonReader.beginArray();
while (forecastJsonReader.hasNext())
String name = forecastJsonReader.nextName();
if (name.equals("date"))
Log.d("WEATHER",forecastJsonReader.nextString());
else if(name.equals("tempMaxC"))
Log.d("WEATHER",forecastJsonReader.nextString());
else
forecastJsonReader.skipValue();
forecastJsonReader.endObject();
forecastJsonReader.close();
【问题讨论】:
【参考方案1】:我认为这不是首先使用 jsonreader 解析 json 的正确方法,您的 json 以对象而不是数组开头 这是一个如何解析 json 的示例
主要功能
reader.beginObject();
while (reader.hasNext())
String name = reader.nextName();
if (name.equals("data"))
readData(reader);
else
reader.skipValue(); // avoid some unhandle events
reader.endObject();
reader.close();
读取数据函数
private void readData(JsonReader reader) throws IOException
reader.beginObject();
while(reader.hasNext())
String name = reader.nextName();
if(name.equals("weather"))
reader.beginArray();
while (reader.hasNext())
reader.beginObject();
String objectWeatherName = reader.nextName();
if (objectWeatherName .equals("date"))
Log.d("WEATHER",reader.nextString());
else if (objectWeatherName .equals("tempMaxC"))
Log.d("WEATHER",reader.nextString());
else
reader.skipValue();
reader.endObject();
reader.endArray();
else
reader.skipValue();
reader.endObject();
如果您想读取位于数据对象中的天气对象中的日期和 tempMaxC,希望您理解我的回答,但如果您有任何问题,请随时在评论中提问:)
【讨论】:
非常感谢您的匹配。您的代码非常有用,但是程序崩溃了 if(name.equals("weather")) reader.beginObject();.Иut 那是另一个问题 事实上,对象“天气”包含数组,所以我们第一次应该使用“reader.beginarray”和“reader.beginobject”。再次感谢。你很有帮助 如何知道数组中有多少对象? @user2013423 抱歉,我错过了 [ 括号现在我已经编辑了我的答案,要知道数组中有多少个对象,您可以计算在 reader.beginArray() 之后的 while reader.hasnext 中有多少循环但是你会被消耗掉,你到底想对数组长度做什么?以上是关于如何使用 JsonReader 跳过不需要的值?的主要内容,如果未能解决你的问题,请参考以下文章
从 gdbinit 脚本单步执行时如何设置跳过不感兴趣的函数?
在搜索 -ldeepsort 时跳过不兼容的 ../tracker/libdeepsort.so