[
{
"configurationId":1,
"configurationName":"CGST",
"configurationVal":"2.5"
},
{
"configurationId":2,
"configurationName":"SGST",
"configurationVal":"2.5"
},
{
"configurationId":3,
"configurationName":"DELIVERY_CHARGE",
"configurationVal":"0"
},
{
"configurationId":4,
"configurationName":"Admin_Mobile",
"configurationVal":"9500100042,8939404592"
},
{
"configurationId":5,
"configurationName":"MIN_ORDER_AMOUNT",
"configurationVal":"98"
},
{
"configurationId":6,
"configurationName":"Packing_charge",
"configurationVal":"3"
},
{
"configurationId":7,
"configurationName":"available_areas",
"configurationVal":"{\"availableAreas\":[{\"area_id\": \"1\",\"area_name\": \"Velachery\",\"pincode\": \"600042\"},{\"area_id\": \"2\",\"area_name\": \"Nanganallur\",\"pincode\": \"600061\"},{\"area_id\": \"3\",\"area_name\": \"Adambakkam\",\"pincode\": \"323546\"},{\"area_id\": \"4\",\"area_name\": \"Madipakkam\",\"pincode\": \"600091\"},{\"area_id\": \"5\",\"area_name\": \"Perungudi\",\"pincode\": \"600096\"},{\"area_id\": \"6\",\"area_name\": \"Pallikarani\",\"pincode\": \"600100\"}]}"
},
{
"configurationId":8,
"configurationName":"order_timing",
"configurationVal":"[{\n\t\"start_time\": \"17:00:00\",\n\t\"end_time\": \"03:00:00\"\n}]"
}
]
答案 0 :(得分:0)
使用以下代码片段解析JsonArray。
String jsonStr = "[{ "configurationId": 1, "configurationName": "CGST",........]";
JSONArray jsonarray = new JSONArray(jsonStr);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
int configurationId = jsonobject.getInt("configurationId");
String configurationName = jsonobject.getString("configurationName");
String configurationVal = jsonobject.getString("configurationVal");
}
最后2个“ configurationVal”是JsonObject本身。因此,您必须再次解析。
希望会有所帮助。
答案 1 :(得分:0)
每当我做很多编码来寻找答案时,我的目标就是确保您真正了解自己在做什么,而不仅仅是将我的代码复制并粘贴到您的项目中。如果您从未使用过instanceof
,建议您继续阅读。如果您有任何问题,请问我。
我会让你开始。为什么?您没有在帖子中显示任何代码,因此我将说明如何处理。
JSONArray object = new JSONArray(jsonData);
//jsonData if the JSON String.
String configVal = null;
JSONArray configArray = null;
JSONArray timingArray = null;
boolean hasAreas = false;
boolean hasTiming = false;
try {
for (int i = 0; i < object.length(); i++) {
JSONObject getJSONObject = object.optJSONObject(i);
if (getJSONObject.get("configurationVal") instanceof JSONObject) {
JSONObject configValObject = getJSONObject.getJSONObject("configurationVal");
configArray = configValObject.getJSONArray("availableAreas");
hasAreas = true;
} else if (getJSONObject.get("configurationVal") instanceof JSONArray) {
timingArray = getJSONObject.getJSONArray("configurationVal");
hasTiming = true;
} else {
configVal = getJSONObject.getString("configurationVal");
}
String configName = getJSONObject.getString("configurationName");
int configId = getJSONObject.getInt("configurationId");
if (hasAreas) {
System.out.println("ConfigVal: " + configVal + ", ConfigName: " + configName + ", ConfigId: " + configId + ", AreaInformation: " + configArray);
}
if (hasTiming) {
System.out.println("ConfigVal: " + configVal + ", ConfigName: " + configName + ", ConfigId: " + configId + ", TimingInfo: " + timingArray);
}
if (!hasAreas && !hasTiming) {
System.out.println("ConfigVal: " + configVal + ", ConfigName: " + configName + ", ConfigId: " + configId);
}
//Reset these...
hasAreas = false;
hasTiming = false;
configVal = null;
}
} catch (JSONException e) {
e.printStackTrace();
}
这将打印出以下内容。
ConfigVal: 2.5, ConfigName: CGST, ConfigId: 1
ConfigVal: 2.5, ConfigName: SGST, ConfigId: 2
ConfigVal: 0, ConfigName: DELIVERY_CHARGE, ConfigId: 3
ConfigVal: 9500100042,8939404592, ConfigName: Admin_Mobile, ConfigId: 4
ConfigVal: 98, ConfigName: MIN_ORDER_AMOUNT, ConfigId: 5
ConfigVal: 3, ConfigName: Packing_charge, ConfigId: 6
ConfigVal: , ConfigName: available_areas, ConfigId: 7, AreaInformation: [{"area_name":"Velachery","pincode":"600042","area_id":"1"},{"area_name":"Nanganallur","pincode":"600061","area_id":"2"},{"area_name":"Adambakkam","pincode":"323546","area_id":"3"},{"area_name":"Madipakkam","pincode":"600091","area_id":"4"},{"area_name":"Perungudi","pincode":"600096","area_id":"5"},{"area_name":"Pallikarani","pincode":"600100","area_id":"6"}]
ConfigVal: , ConfigName: order_timing, ConfigId: 8, TimingInfo: [{"start_time":"17: 00: 00","end_time ":"03: 00: 00"}]
现在,您可以看到,未解析available_areas和order_timing。但是,我确实给了您JSONArray中的信息。这样您就可以按照我的所作所为进行分析。但是,我将为此使用带有List <>的模型。使用起来会更容易。