无法在LinkedHashMap的Arraylist中获取JSON响应

时间:2013-07-26 19:37:58

标签: android json

我有来自服务器的这种类型的JSON响应......

{
    "Field1": {
        "Field Name": "Phone Number",
        "Field Type": "Number",
        "Validation": {
            "Min": "10",
            "Max": "12"
        }
    },
    "Field2": {
        "Field Name": "Email",
        "Field Type": "String",
        "Validation": {
            "Regular Expression": "XltfQS1aYS16MC05LVxcK10rKFxcLltfQS1aYS16MC05LV0rKSpAIiAJCSsgIltBLVphLXowLTktXSsoXFwuW0EtWmEtejAtOV0rKSooXFwuW0EtWmEtel17Mix9KSQ=",
            "Min Length": "20",
            "Max Length": "50"
        }
    },
    "Field3": {
        "Field Name": "Amount",
        "Field Type": "Decimal",
        "Validation": {
            "Number Precesion": "2",
            "Min": "2",
            "Max": "4"
        }
    },
    "Field4": {
        "Field Name": "Date",
        "Field Type": "Date",
        "Validation": ""
    },
    "Field5": {
        "Field Name": "Date_and_Time",
        "Field Type": "Date And Time",
        "Validation": ""
    }
}

现在我想解析这个没有密钥名称的JSON Reponse。 为此,我尝试过这样::

public ArrayList<LinkedHashMap<String, String>> getDynamicFieldForBillPay() throws JSONException
    {
        ArrayList<LinkedHashMap<String, String>> allData = new ArrayList<LinkedHashMap<String, String>>();
        LinkedHashMap<String, String> dynamicValues = new LinkedHashMap<String, String>();

        String fieldInfo="";
        BillPayProduct billpay=new BillPayProduct();

        DataHelper datahelper=new DataHelper(context);
        billpay=datahelper.getBillPayProductInfo();

        fieldInfo=billpay.getFieldInfo();

        System.out.println(" $$$$   "+fieldInfo);
        String fieldName=null,fieldType=null;

        JSONObject json;
        JSONArray jsonarr;

        JSONObject parentObject = new JSONObject(fieldInfo);
        try
        {
            if (fieldInfo.startsWith("{")) 
            {
                json = new JSONObject(fieldInfo);
                Iterator<String> it = json.keys();
                while (it.hasNext()) 
                {
                    String key = it.next();
                    String value = json.getString(key);

                    System.out.println(" ^^^^ KEY==>"+key);
                    System.out.println(" ^^^^ VALUE==>"+value);
                    dynamicValues.put(key, value);
                }
                allData.add(dynamicValues);
            } 
            else if (fieldInfo.startsWith("[")) 
            {
                jsonarr = new JSONArray(fieldInfo);
                for (int i = 0; i < jsonarr.length(); i++) 
                {
                    dynamicValues = new LinkedHashMap<String, String>();
                    json = (JSONObject) jsonarr.get(i);
                    Iterator<String> it = json.keys();

                    while (it.hasNext()) 
                    {
                        String key = it.next();
                        String value = json.getString(key);
                        dynamicValues.put(key, value);
                    }

                    System.out.println(" *********** Value Added  ===>"+allData.add(dynamicValues));
                    allData.add(dynamicValues);
                }
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return allData;
    }

这将返回我的输出::

KEY==>Field3

VALUE==>{"Validation":{"Number Precesion":"2","Max":"4","Min":"2"},"Field Type":"Decimal","Field Name":"Amount"}
KEY==>Field2
VALUE==>{"Validation":{"Min Length":"20","Regular Expression":"XltfQS1aYS16MC05LVxcK10rKFxcLltfQS1aYS16MC05LV0rKSpAIiAJCSsgIltBLVphLXowLTktXSsoXFwuW0EtWmEtejAtOV0rKSooXFwuW0EtWmEtel17Mix9KSQ=","Max Length":"50"},"Field Type":"String","Field Name":"Email"}

KEY==>Field5

VALUE==>{"Validation":"","Field Type":"Date And Time","Field Name":"Date_and_Time"}

KEY==>Field4
VALUE==>{"Validation":"","Field Type":"Date","Field Name":"Date"}

KEY==>Field1

VALUE==>{"Validation":{"Max":"12","Min":"10"},"Field Type":"Number","Field Name":"Phone Number"}

我想要每个值的输出......

类似于关键字段3具有值

  • 最小= 2
  • 最大= 4
  • FieldType = Decimal等。

同样的方式我如何在解析结果并将其设置为linkedhash map的Arraylist(不使用其键名)后使用它?

任何帮助将不胜感激???

先谢谢... :)

1 个答案:

答案 0 :(得分:0)

您可以使用droidQuery更轻松地解析JSON

鉴于您的String JSON:private String string = "{...}";

try {
    JSONObject json = $.parseJSON(string);
    Map<String, ?> map = $.map(json);
    for (Entry<String, ?> entry : map.entrySet()) {
        JSONObject obj = (JSONObject) entry.getValue();
        Map<String, ?> innerMap = $.map(obj);
        for (Entry<String, ?> innerEntry : innerMap.entrySet()) {
            Object val = innerEntry.getValue();
            if (val instanceof JSONObject) {
                for (Entry<String, ?> value : $.map((JSONObject) val).entrySet()) {
                    System.out.println(value.getKey() + " = " + value.getValue());
                }
            }
            else {
                System.out.println(innerEntry.getKey() + " = " + val);
            }
        }
    }
} catch (Throwable t) {
    t.printStackTrace();
}