猿问

将字符串转换为JSON数组

将字符串转换为JSON数组

我从Web服务获得以下字符串的JSON,并尝试将其转换为 JSONarray

{
    "locations": [
        {
            "lat": "23.053",
            "long": "72.629",
            "location": "ABC",
            "address": "DEF",
            "city": "Ahmedabad",
            "state": "Gujrat",
            "phonenumber": "1234567"
        },
        {
            "lat": "23.053",
            "long": "72.629",
            "location": "ABC",
            "address": "DEF",
            "city": "Ahmedabad",
            "state": "Gujrat",
            "phonenumber": "1234567"
        },
        {
            "lat": "23.053",
            "long": "72.629",
            "location": "ABC",
            "address": "DEF",
            "city": "Ahmedabad",
            "state": "Gujrat",
            "phonenumber": "1234567"
        },
        {
            "lat": "23.053",
            "long": "72.629",
            "location": "ABC",
            "address": "DEF",
            "city": "Ahmedabad",
            "state": "Gujrat",
            "phonenumber": "1234567"
        },
        {
            "lat": "23.053",
            "long": "72.629",
            "location": "ABC",
            "address": "DEF",
            "city": "Ahmedabad",
            "state": "Gujrat",
            "phonenumber": "1234567"
        }
    ]}

我在String网上验证了这一点,似乎是正确的。现在我在android开发中使用以下代码来利用

JSONArray jsonArray = new JSONArray(readlocationFeed);

这会抛出异常类型不匹配的异常。


LEATH
浏览 1685回答 3
3回答

暮色呼如

在这里你得到JSONObject所以改变这一行:JSONArray&nbsp;jsonArray&nbsp;=&nbsp;new&nbsp;JSONArray(readlocationFeed);以下内容:JSONObject&nbsp;jsnobject&nbsp;=&nbsp;new&nbsp;JSONObject(readlocationFeed);之后JSONArray&nbsp;jsonArray&nbsp;=&nbsp;jsnobject.getJSONArray("locations");for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;<&nbsp;jsonArray.length();&nbsp;i++)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;JSONObject&nbsp;explrObject&nbsp;=&nbsp;jsonArray.getJSONObject(i);}

慕神8447489

输入字符串[ &nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"userName":&nbsp;"sandeep", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"age":&nbsp;30 &nbsp;&nbsp;&nbsp;},&nbsp; &nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"userName":&nbsp;"vivan", &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"age":&nbsp;5 &nbsp;&nbsp;&nbsp;}]将字符串转换为JSON的简单方法public&nbsp;class&nbsp;Test{ &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;main(String[]&nbsp;args)&nbsp;throws&nbsp;JSONException &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;data&nbsp;=&nbsp;"[{\"userName\":&nbsp;\"sandeep\",\"age\":30},{\"userName\":&nbsp;\"vivan\",\"age\":5}]&nbsp;&nbsp;"; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JSONArray&nbsp;jsonArr&nbsp;=&nbsp;new&nbsp;JSONArray(data); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;<&nbsp;jsonArr.length();&nbsp;i++) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JSONObject&nbsp;jsonObj&nbsp;=&nbsp;jsonArr.getJSONObject(i); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(jsonObj); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;}}产量{"userName":"sandeep","age":30}{"userName":"vivan","age":5}

MYYA

使用json lib: -String&nbsp;data="[{"A":"a","B":"b","C":"c","D":"d","E":"e","F":"f","G":"g"}]";Object&nbsp;object=null;JSONArray&nbsp;arrayObj=null;JSONParser&nbsp;jsonParser=new&nbsp;JSONParser();object=jsonParser.parse(data);arrayObj=(JSONArray)&nbsp;object;System.out.println("Json&nbsp;object&nbsp;::&nbsp;"+arrayObj);使用GSON lib: -Gson&nbsp;gson&nbsp;=&nbsp;new&nbsp;Gson();String&nbsp;data="[{\"A\":\"a\",\"B\":\"b\",\"C\":\"c\",\"D\":\"d\",\"E\":\"e\",\"F\":\"f\",\"G\":\"g\"}]";JsonParser&nbsp;jsonParser&nbsp;=&nbsp;new&nbsp;JsonParser();JsonArray&nbsp;jsonArray&nbsp;=&nbsp;(JsonArray)&nbsp;jsonParser.parse(data);
随时随地看视频慕课网APP

相关分类

Java
Android
我要回答