我有来自服务器的这种类型的 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 响应。为此,我尝试过这样::
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"}
我想要每个值的输出......
像 Key Field 3 一样有值
- 最小值=2
- 最大=4
- FieldType=十进制等。
同样的方式,在解析结果并将其设置为linkedhash映射的Arraylist(不使用其键名)后如何使用它?
任何帮助将不胜感激 ???
提前致谢 ... :)