简体   繁体   English

如何在android中访问JSON对象内的数组内容

[英]How access array content inside JSON objects in android

I am getting the following JSON response using volley in android but, I am not able to access the contents of Fields array (ie I want access "type" in that array)我在 android 中使用 volley 得到以下 JSON 响应,但是,我无法访问 Fields 数组的内容(即我想访问该数组中的“类型”)

Here is my response from volley这是我对凌空的回应

{
        "namelabels": "{\"text1\": \"Text 1\", \"int1\": \"Integer 1\", \"Name\": \"Label\", \"decimal1\": \"Decimal 1\"}",
        "customer_id": 13,
        "modified_by": 264,
        "prj_name": "Construction X",
        "status_id": 1,
        "created_by_name": "Sudhir Murthy",
        "form_desc": "Single screen form",
        "created_by": 264,
        "project_id": "59193e4e359dcf7c67001bea",
        "created_on": "2017-09-25 10:04:33.654000",
        "frm_state": "Active",
        "modified_on": "2017-09-25 10:04",
        "form_name": "Single screen",
        "form_json": "{\"1\": {\"type\": \"page\", \"Fields\": [{\"read_only\": \"\", \"constraint\": \"\", \"relevant\": \"\",
    \"name\": \"text1\", \"constraint_message\": \"\", \"hint\": \"\",
    \"calculation\": \"\", \"required\": \"\", \"appearance\": \"\",
    \"label\": \"Text 1\", \"default\": \"\", \"type\": \"text\"},
    {\"read_only\": \"\", \"constraint\": \"\", \"relevant\": \"\",
    \"name\": \"int1\", \"constraint_message\": \"\", \"hint\": \"\",
    \"calculation\": \"\", \"required\": \"\", \"appearance\": \"\",
    \"label\": \"Integer 1\", \"default\": \"\", \"type\": \"integer\"},
    {\"read_only\": \"\", \"constraint\": \"\", \"relevant\": \"\",
    \"name\": \"decimal1\", \"constraint_message\": \"\", \"hint\":
    \"\", \"calculation\": \"\", \"required\": \"\", \"appearance\":
    \"\", \"label\": \"Decimal 1\", \"default\": \"\", \"type\":
    \"decimal\"}], \"appearance\": \"field-list\", \"name\": \"\",
    \"goto\": 2}}",
        "id": "59c88759a2b6b541e549c2e0",
        "xls_file_url": "https://storage.googleapis.com/4c28c064-7d88-11e6-bafd-f23c91e268d3/59193e4e359dcf7c67001bea/d3a62f6f-7841-4407-891b-4558a7711659.xls"
    }

NOTE: Fields array is in "form_json": "{\\"1\\": {\\"type\\": \\"page\\", \\"Fields\\"注意:字段数组在 "form_json": "{\\"1\\": {\\"type\\": \\"page\\", \\"Fields\\"

Can anybody please help me by posting code snippet to access the array.任何人都可以通过发布代码片段来访问数组来帮助我。

NOTE:I tried this method but it is not working注意:我试过这个方法,但它不起作用

public static void parseJSON(String response) throws JSONException
{
    JSONObject jsonObject = new JSONObject(response);
    JSONObject myResponse = jsonObject.getJSONObject("form_json").getJSONObject("1");

    JSONArray fieldObjects = (JSONArray) myResponse.get("Fields");

    ArrayList<String> list = new ArrayList<String>();

    for(int i=0; i<fieldObjects.length(); i++){
        list.add(fieldObjects.getJSONObject(i).getString("type"));
    }

    Log.d("newlist", String.valueOf(list));
}

我认为您的 json 字符串是错误的。请检查您的响应数据。

you have json and another json as content of "form_json" this another json is encoded and after deconding looks like: (decoding in this case means - remove all '\\')你有 json 和另一个 json 作为“form_json”的内容,另一个 json 被编码,解码后看起来像:(在这种情况下解码意味着 - 删除所有 '\\')

{
  "type": "page",
  "Fields": [
    {
      "read_only": "",
      "constraint": "",
      "relevant": "",
      "name": "text1",
      "constraint_message": "",
      "hint": "",
      "calculation": "",
      "required": "",
      "appearance": "",
      "label": "Text 1",
      "default": "",
      "type": "text"
    },
    {
      "read_only": "",
      "constraint": "",
      "relevant": "",
      "name": "int1",
      "constraint_message": "",
      "hint": "",
      "calculation": "",
      "required": "",
      "appearance": "",
      "label": "Integer 1",
      "default": "",
      "type": "integer"
    },
    {
      "read_only": "",
      "constraint": "",
      "relevant": "",
      "name": "decimal1",
      "constraint_message": "",
      "hint": "",
      "calculation": "",
      "required": "",
      "appearance": "",
      "label": "Decimal 1",
      "default": "",
      "type": "decimal"
    }
  ],
  "appearance": "field-list",
  "name": "",
  "goto": 2
}
JSONObject jsonObject = new JSONObject(response);

JSONObject myResponse = new JSONObject(jsonObject.getString("form_json")).getJSONObject("1");

JSONArray fieldObjects =  myResponse.getJSONArray("Fields");

ArrayList<String> list = new ArrayList<String>();

for (int i = 0; i < fieldObjects.length(); i++) {
    list.add(fieldObjects.getJSONObject(i).getString("type"));
}

Log.d("newlist", list.toString());

You can find your Json responce valid or not.您可以发现您的Json responce有效。 https://jsonlint.com/ https://jsonlint.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM