繁体   English   中英

JsonObject JsonArray解析问题

[英]JsonObject JsonArray parsing issue

我是JSON的新手,但已经成功地从其他JSON请求中提取数据。 这给我带来麻烦。 任何帮助或指针,将不胜感激。

这是JSON请求: http : //api.wunderground.com/api/95e20de6002dc6f0/currenthurricane/view.json

以上就是我想要的数据。

以下是我遇到问题的代码:

public static ArrayList<CycloneData> extractFeatureFromJson(String cycloneJSON) {

    // Create an empty ArrayList to start adding Cyclones to
    ArrayList<CycloneData> cyclones = new ArrayList<>();

    // try to parse the cycloneJSON response string. If there's a problem with the way the JSON
    // is formatted, a JSONException exception object will be thrown.
    // Catch the exception, and print the error message to the logs.

    try {

        JSONObject rootJsonObject = new JSONObject(cycloneJSON);

        // Create JSONArray associated with the key called "currenthurricane", which represents
        // a list of cyclones from JSON response.
        JSONArray currentHurricaneArray = rootJsonObject.getJSONArray("currenthurricane");

        //Loop through each section in the currentHurricaneArray array & create an
        //{@link CycloneData} object for each one
        for (int i = 0; i < currentHurricaneArray.length(); i++) {
            //Get cyclone JSONObject at position i in the array
            JSONObject cycloneProperties = currentHurricaneArray.getJSONObject(i);
            //Extract “stormName_Nice” for Cyclone's name
            String name = cycloneProperties.optString("stormName_Nice");
            // Extract the value for the key called "url"
            String url = cycloneProperties.optString("url");
            int category = cycloneProperties.optInt("SaffirSimpsonCategory");
            CycloneData cyclone = new CycloneData(category, name, url);
            //Add new cyclone to list
            cyclones.add(cyclone);
        }

    } catch (JSONException e) {
        // If an error is thrown when executing any of the above statements in the "try" block,
        // catch the exception here, so the app doesn't crash. Print a log message
        // with the message from the exception.
        Log.e("Utils", "Problem parsing the cyclone JSON results", e);
    }

    // Return the list of cyclones
    return cyclones;
}

使用Android Studio中的调试器,我可以在以下代码中看到currentHurricaneArray: JSONArray currentHurricaneArray = rootJsonObject.getJSONArray("currenthurricane");

正在获取预期的JSON数组数据。

当for循环启动JSONObject时: JSONObject cycloneProperties = currentHurricaneArray.getJSONObject(i);

以及我正在寻找的正确数组信息。

但是,此后开始提取字符串。 String name = cycloneProperties.optString("stormName_Nice");

它什么也不返回。

调试显示: name = ""

如果使用JSON查询工具,我可以获得所需的信息,但是我不知道如何在代码中使用它。

我确定我的String提取错误,只是无法弄清楚如何正确处理。 也许我一直都错了。

*************下面的好代码*******************

好吧,GaëtanMaisse让我走了。 以下是我为使其正常工作所做的事情。

for (int i = 0; i < currentHurricaneArray.length(); i++) {
            //Get cyclone JSONObject at position i in the array
            JSONObject cycloneProperties = currentHurricaneArray.getJSONObject(i);

            // Extract "stormInfo" object
            JSONObject stormInfo = cycloneProperties.optJSONObject("stormInfo");
            //Extract “stormName_Nice” & "requesturl" for Cyclone's name and url
            String name = stormInfo.optString("stormName_Nice");
            String url = stormInfo.optString("requesturl");

            // Extract "Current" object
            JSONObject Current = cycloneProperties.optJSONObject("Current");
            // Extract "SaffirSimpsonCategory" key
            int category = Current.optInt("SaffirSimpsonCategory");

            CycloneData cyclone = new CycloneData(category, name, url);
            //Add new cyclone to list
            cyclones.add(cyclone);
        }

optString(String name,String fallback),返回按名称映射的值(如果存在),如果需要则强制转换,如果没有这种映射则返回。 打印回退到测试。如果回退被触发,则没有密钥。这表明您的json结构格式错误或您的解析逻辑不适合您使用的已定义结构。

您在stormInfo的解析过程中缺少JSON密钥stormName_Nice

{
    "response": {
        "version": "0.1",
        "termsofService": "http://www.wunderground.com/weather/api/d/terms.html",
        "features": {
            "currenthurricane": 1
        }
    },
    "currenthurricane": [{
        "stormInfo": {
            "stormName": "Daniel",
            "stormName_Nice": "Hurricane Daniel",
            "stormNumber": "ep201204"
        },
        ...,
        "SaffirSimpsonCategory": 1,
        "url":"URL",
        ... 
    }]
}

它应该更好地与此配合使用:

JSONObject rootJsonObject = new JSONObject(cycloneJSON);

// Create JSONArray associated with the key called "currenthurricane", which represents
// a list of cyclones from JSON response.
JSONArray currentHurricaneArray = rootJsonObject.getJSONArray("currenthurricane");

//Loop through each section in the currentHurricaneArray array & create an
//{@link CycloneData} object for each one
for (int i = 0; i < currentHurricaneArray.length(); i++) {
    //Get cyclone JSONObject at position i in the array
    JSONObject cycloneProperties = currentHurricaneArray.getJSONObject(i);

    // Extract "stormInfo" object
    JSONObject stormInfo = cycloneProperties.getJSONObject("stormInfo");
    //Extract “stormName_Nice” for Cyclone's name
    String name = stormInfo.optString("stormName_Nice");

    // Extract other values from cycloneProperties
    String url = cycloneProperties.optString("url");
    int category = cycloneProperties.optInt("SaffirSimpsonCategory");
    CycloneData cyclone = new CycloneData(category, name, url);
    //Add new cyclone to list
    cyclones.add(cyclone);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM