字符串转换为JsonArray转换为、字符串、JsonArray

由网友(浪冢)分享简介:如何正确此字符串转换为jsonArray?how to properly convert this String to a jsonArray?{"myArray": [{ "id": 1,"att1": 14.2,"att2": false },{ "id": 2,"att1": 13.2,"att2":...

如何正确此字符串转换为jsonArray?

how to properly convert this String to a jsonArray?

{
    "myArray": [
    {   "id": 1,
        "att1": 14.2,
        "att2": false },
    {   "id": 2,
        "att1": 13.2,
        "att2": false },
    {   "id": 3,
        "att1": 13,
        "att2": false }
  ]}

这是 JSONArray jArray =新JSONArray(STRING_FROM_ABOVE); 的结果 jArray.length = 1 它的我第一次取得联系JSON:)

An JSONArray jArray = new JSONArray(STRING_FROM_ABOVE); results in jArray.length = 1 Its my first time to get in touch with json :)

推荐答案

试试这个:

JSONObject jObject = new JSONObject(STRING_FROM_ABOVE);
JSONArray jArray = jObject.getJSONArray("myArray");

在string_from_above不是一个JSON数组,这是一个JSON对象,包含一个属性(myarray的),这是一个JSON数组;)

The "string_from_above" is not a Json Array, it's a Json object, containing one attribute (myArray) which is a Json Array ;)

您可以然后执行:

for (int i = 0; i < jArray.length(); i++) {
        JSONObject jObj = jArray.getJSONObject(i);
        System.out.println(i + " id : " + jObj.getInt("id"));
        System.out.println(i + " att1 : " + jObj.getDouble("att1"));
        System.out.println(i + " att2 : " + jObj.getBoolean("att2"));
}
阅读全文

相关推荐

最新文章