1

我有这样的 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。

任何帮助将不胜感激。

提前致谢 ... :)

4

1 回答 1

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();
}  
于 2013-07-25T06:03:03.707 回答