Reading from JSON on Android

后端 未结 1 434
北荒
北荒 2021-01-27 16:36

I have created an string in JSON format. It looks like:

private String jString = \"{\\\"CategoryId\\\":1},{\\\"CategoryId\\\":2}\";

and this is

相关标签:
1条回答
  • 2021-01-27 17:11

    This is not a valid JSON format. Look here to see the format of a JSON object / array.

    If you want to be able to read both values, create an array:

    String jString = "[{\"CategoryId\":1},{\"CategoryId\":2}]";
    JSONArray jArr = new JSONArray(jString);
    for (int i = 0; i < jArr.length(); ++i) {
        int i = jArr.getJsonObject(i).getInt("CategoryId");
        // do something with i which is an int, not a String
    }
    
    0 讨论(0)
提交回复
热议问题