作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 JSON 解析器上遇到了问题。这是来自服务器的 JSON 响应。
{
"coord" : {"lon":37.62,"lat":55.75},
"weather":[{"id":803,"main":"Clouds","description":"test","icon":"04d"}],
"base" :"stations",
"main" :{"temp":12.76,"pressure":1007,"humidity":93,"tempmin":12,"tempmax":14},
"visibility":6000,
"wind" :{"speed":4,"deg":300},
"clouds" :{"all":75},
"dt":1504881000,
"sys" :{"type":1,"id":7325,"message":0.0064,"country":"RU","sunrise":1504838942,"sunset":1504886617},
"id" :524901,
"name" :"City",
"cod" :200
}
和java代码....
import org.json.JSONException;
import org.json.JSONObject;
import com.google.gson.*;
public static void main(String[] args) throws JSONException {
try {
JsonParser parser = new JsonParser();
JsonObject json = parser.parse("JSON responce here").getAsJsonObject();
JsonArray weather = json.get("weather").getAsJsonArray(); //no problem
int visibility = json.get("visibility").getAsInt();
int id = json.get("id").getAsInt();
int dt = json.get("dt").getAsInt();
String name = json.get("name").getAsString();
JsonArray clouds = json.get("clouds").getAsJsonArray(); //here is the problem
JsonArray main = json.get("main").getAsJsonArray(); //here is the problem
} catch (Exception e) {
e.printStackTrace();
}
}
问题是……当我编译时,我在 JsonArray clouds = json.get("clouds").getAsJsonArray(); 和其他类似的行。
但是 JsonArray weather = json.get("weather").getAsJsonArray(); 没问题...
我不明白发生了什么……但是数组“天气”节点没有问题……完全没有问题。请帮帮我……怎么了?
最佳答案
因为是Json对象
JsonObject json = json.get("clouds").getAsJsonObject()
它会工作...
或者您可以按照下面给出的方式更改数据
{
"coord" : {"lon":37.62,"lat":55.75},
"weather":{"id":803,"main":"Clouds","description":"test","icon":"04d"},
"base" :"stations",
"main" :{"temp":12.76,"pressure":1007,"humidity":93,"tempmin":12,"tempmax":14},
"visibility":6000,
"wind" :{"speed":4,"deg":300},
"clouds" :[{"all":75}],
"dt":1504881000,
"sys" :{"type":1,"id":7325,"message":0.0064,"country":"RU","sunrise":1504838942,"sunset":1504886617},
"id" :524901,
"name" :"City",
"cod" :200
}
关于java - 解析 JSON 时,出现奇怪的异常 :This is not a JSON Array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46122678/
我是一名优秀的程序员,十分优秀!