将字符串转换为jsonArray

时间:2018-06-21 13:12:07

标签: java android json

我的字符串:

String bu =  "[{\"name\":\"Personal Care\",\"created_at\":\"2018-04-29T10:55:37+00:00\",\"id\":2},{\"name\":\"Domestic Appliances\",\"created_at\":\"2017-04-23T19:47:39+00:00\",\"id\":3},{\"name\":\"Coffee\",\"created_at\":\"2017-11-08T20:46:14+00:00\",\"id\":4}]"

我正在使用的将字符串转换为JsonArray的方法:

JsonParser jsonParser = new JsonParser();
JsonElement jsonElement = jsonParser.parse(bu);
Log.v("s" , jsonElement.getAsString());
JsonArray jsonArray = jsonElement.getAsJsonArray();

我在日志中出现错误:

06-21 18:39:43.265 9128-9128/com.harit.abs E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.harit.abs, PID: 9128
java.lang.IllegalStateException: This is not a JSON Array.
    at com.google.gson.JsonElement.getAsJsonArray(JsonElement.java:106)
    at com.harit.abs.Sync_Activity$2.onCompleted(Sync_Activity.java:95)
    at com.harit.abs.Sync_Activity$2.onCompleted(Sync_Activity.java:80)
    at com.koushikdutta.async.future.SimpleFuture.handleCallbackUnlocked(SimpleFuture.java:107)
    at com.koushikdutta.async.future.SimpleFuture.setComplete(SimpleFuture.java:141)
    at com.koushikdutta.async.future.SimpleFuture.setComplete(SimpleFuture.java:128)
    at com.koushikdutta.ion.IonRequestBuilder$1.run(IonRequestBuilder.java:246)
    at com.koushikdutta.async.AsyncServer$RunnableWrapper.run(AsyncServer.java:60)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

2 个答案:

答案 0 :(得分:0)

这里您得到JSONObject,请更改

JSONArray jsonArray = new JSONArray(readlocationFeed); 

以下行

JSONObject jsnobject = new JSONObject(readlocationFeed);

及之后

JSONArray jsonArray = jsnobject.getJSONArray("locations");
for (int i = 0; i < jsonArray.length(); i++) {
    JSONObject explrObject = jsonArray.getJSONObject(i);
}

答案 1 :(得分:0)

那是一个JSONArray...。您也可以尝试类似的方法……以我的观点,这当然更简单。

尝试一下。

  

字符串bu =“ [{\” name \“:\”个人   Care \“,\” created_at \“:\” 2018-04-29T10:55:37 + 00:00 \“,\” id \“:2},{\” name \“:\” Domestic   设备\“,\” created_at \“:\” 2017-04-23T19:47:39 + 00:00 \“,\” id \“:3},{\”名称\“:\”咖啡\“, \“ created_at \”:\“ 2017-11-08T20:46:14 + 00:00 \”,\“ id \”:4}]“;

try {
        JSONArray array = new JSONArray(bu);
        System.out.println(array);

        for (int i = 0; i < array.length(); i++) {
            System.out.println(array.getJSONObject(i).getString("name"));
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

上面的代码将打印出以下内容:

  

[{“ name”:“个人   护理”,“ created_at”:“ 2018-04-29T10:55:37 + 00:00”,“ id”:2},{“ name”:“国内   设备”,“ created_at”:“ 2017-04-23T19:47:39 + 00:00”,“ id”:3},{“名称”:“咖啡”,“ created_at”:“ 2017-11-08T20: 46:14 + 00:00“,” id“:4}]

     

个人护理

     

国内

     

电器

     

咖啡