如何解析JSON对象在Andro

如何解析JSON对象在Andro

本文介绍了如何解析JSON对象在Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些问题,从JSON对象拉值。这是我的code

 尝试{
    JSONObject的JSON =新的JSONObject的(结果);
    JSONObject的json2 = json.getJSONObject(结果);
    测试= json2.getString(姓名);
}赶上(JSONException E){
    e.printStackTrace();
}
 

测试声明为字符串。当code运行它显示。如果我将鼠标悬停在 json2 在调试模式下,我可以看到该对象中的所有值和名称。

我也试过

 测试= json2.length();
 

这回测试= 0 。甚至当我在 json2 物体盘旋,我可以在对象中读出值。

下面是我将使用一个JSON字符串的例子。

  {
    来电:getPoiById
    结果:
    {
        indexForPhone:0,
        indexForEmail:空,
        indexForHomePage:空,
        indexForComment:空,
        手机:05137-930 68,
        cleanPhone:0513793068,
        internetAccess:2,
        overnightStay:2,
        wasteDisposal:2,
        厕所:2,
        电:2,
        CRAN:2,
        滑道:2,
        野营:2,
        淡水:2,
        fieldNamesWithValue:电话]
        fieldNameTranslations:电话]
        ID:1470,
        名:滨海Rasche船厂有限公司和放大器; Co. KG的,
        纬度:52.3956107286487
        经度:9.56583023071289
    }
}
 

解决方案

在最后我解决了它使用 JSONObject.get ,而不是的JSONObject .getString 再投测试字符串

 私人无效SAVEDATA(字符串结果){
    尝试 {
        JSONObject的JSON =(JSONObject的)新JSONTokener(结果).nextValue();
        JSONObject的json2 = json.getJSONObject(结果);
        测试=(字符串)json2.get(姓名);
    }赶上(JSONException E){
        e.printStackTrace();
    }
}
 

I am having some problems pulling values from a JSON object. Here is my code

try {
    JSONObject json = new JSONObject(result);
    JSONObject json2 = json.getJSONObject("results");
    test = json2.getString("name");
} catch (JSONException e) {
    e.printStackTrace();
}

test is declared as a String. When the code runs it is showing null. If I hover over json2 in debug mode I can see all the values and names within the object.

I also tried

test = json2.length();

This returned test = 0. Even when I hover over the json2 object I can read the values within the object.

Here is an example of a JSON string I will use.

{
    "caller":"getPoiById",
    "results":
    {
        "indexForPhone":0,
        "indexForEmail":"NULL",
        "indexForHomePage":"NULL",
        "indexForComment":"NULL",
        "phone":"05137-930 68",
        "cleanPhone":"0513793068",
        "internetAccess":"2",
        "overnightStay":"2",
        "wasteDisposal":"2",
        "toilet":"2",
        "electricity":"2",
        "cran":"2",
        "slipway":"2",
        "camping":"2",
        "freshWater":"2",
        "fieldNamesWithValue":["phone"],
        "fieldNameTranslations": ["Telefon"],
        "id":"1470",
        "name":"Marina Rasche Werft GmbH & Co. KG",
        "latitude":"52.3956107286487",
        "longitude":"9.56583023071289"
    }
}
解决方案

In the end I solved it by using JSONObject.get rather than JSONObject.getString and then cast test to a String.

private void saveData(String result) {
    try {
        JSONObject json= (JSONObject) new JSONTokener(result).nextValue();
        JSONObject json2 = json.getJSONObject("results");
        test = (String) json2.get("name");
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

这篇关于如何解析JSON对象在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:08