无法解析JSON响应

时间:2013-07-25 05:45:41

标签: android json

我有像这样的JSON响应。我已经尝试了很多但无法解析值。

这是我的实际回应......

{"ResponseCode":"000","ResponseDescription":"Successful","SystemServiceID":["0000"],"SystemServiceName":["Test"],"ProductID":["000"],"ProductName":["Test"],"ProductDescription":["Test product"],"MinimumValue":[10000],"MaximumValue":[500000],"ImageURL":[null],"Country":["AAAA"],"CompanyID":["1"],"CompanyName":["Test"],"FieldLevel":["2"],"FieldInfo":["{\"Field1\":{\"Field Name\":\"Phone Number\",\"Field Type\":\"Number\",\"Validation\":{\"Min\":\"4\",\"Max\":\"8\"}},\"Field2\":{\"Field Name\":\"Email\",\"Field Type\":\"String\",\"Validation\":{\"Regular Expression\":\"abcd\",\"Min Length\":\"10\",\"Max Length\":\"20\"}}}"]}

出于这个原因,我可以解析所有字段,期望下面的字段......

{
    "Field1":
    {
            "Field Name":"Phone Number",
            "Field Type":"Number",
            "Validation":{"Min":"4","Max":"8"}
    },

    "Field2":
    {
            "Field Name":"Email",
            "Field Type":"String",
            "Validation":{"Regular Expression":"abcd","Min Length":"10","Max Length":"20"}
    }
}

我还想获取Validation":{"Min":"4","Max":"8"}此字段的值。例如,它的最大值为4,最小值为8.

任何帮助将不胜感激。

先谢谢... :)

1 个答案:

答案 0 :(得分:3)

以下是读取最小值和最大值

的代码
JSONObject parentObject = new JSONObject(Json_String); 

JSONObject field1 = parentObject.getJSONObject("Field1"); 
JSONObject validation = field1.getJSONObject("Validation"); 

String min = validation.getString("Min"); 
String max = validation.getString("Max"); 

假设您的JSON字符串格式正确。

由于在您的回复中,您没有将"\"转换为JSON,因此您需要对其进行一些修改(更新:此处似乎不需要替换)

String json = "{\"Field1\":{\"Field Name\":\"Phone Number\",\"Field Type\":\"Number\",\"Validation\":{\"Min\":\"4\",\"Max\":\"8\"}},\"Field2\":{\"Field Name\":\"Email\",\"Field Type\":\"String\",\"Validation\":{\"Regular Expression\":\"abcd\",\"Min Length\":\"10\",\"Max Length\":\"20\"}}}";

// json.replace("\\", ""); // I verified that parsing works well without replacement too

try {
     JSONObject parentObject = new JSONObject(json);
     JSONObject field1 = parentObject.getJSONObject("Field1"); 
     JSONObject validation = field1.getJSONObject("Validation"); 

     String min = validation.getString("Min"); 
     String max = validation.getString("Max");

     System.out.println("Min :::::::  " + min);
     System.out.println("MAx ::::::: " + max);
    } catch (JSONException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
}