我有像这样的JSON
{
"result":
{
"issue_date": "xx-yy-zzzz",
"father/husband": "TEST",
"name": "ABC ",
"blood_group": "",
"dob": "xx-yy-zzzz",
"validity": {
"non-transport": "xx-yy-zzzz to xx-yy-zzzz",
"transport": "xx-yy-zzzz to xx-yy-zzzz"
},
"cov_details": {
"MCWG": "NA",
"3WTR": "NA",
"PSV BUS": "NA",
"LMV": "NA",
"INVCRG": "NA"
},
"address": "ABC"
}
}
JSONObject dlData = new JSONObject();
JSONObject dlObj = new JSONObject();
JSONObject dlcov = new JSONObject();
JSONObject dlCovs = new JSONObject();
dlCov = jsonObject.getJSONObject("result").getJSONObject("cov_details");
访问cov_details的数据, 我正在使用此代码块来存储cov_details对象
中的详细信息 dlcov = jsonObject.getJSONObject("result").getJSONObject("cov_details");
Iterator<String> x = dlcov.keys();
while (x.hasNext()){
String key1 = x.next();
String value1 = dlcov.optString(key1);
dlCovs.put("covabbrv",key1);
dlCovs.put("dcIssuedt",value1);
dlCovs.put("vecatg",key1);
}
dlData.put("dlCovs", dlCovs);
我正在尝试将每个值存储在dlCovs中,但它只存储对象中的最后一个值,无论如何使用它我可以使用它的Key值存储dlCovs对象中的所有值并迭代它。任何帮助将是高度的可感谢,提前谢谢。 在这里输入代码
答案 0 :(得分:1)
将JSONArray
用于covabbrv,dcIssuedt和vecatg
JSONArray covabbrv = new JSONArray();
JSONArray dcIssuedt = new JSONArray();
JSONArray vecatg = new JSONArray();
Iterator<String> x = dlcov.keys();
while (x.hasNext()){
String key1 = x.next();
String value1 = dlcov.optString(key1);
covabbrv.put(key1);
dcIssuedt.put(value1);
vecatg.put(key1);
}
dlCovs.put("covabbrv",covabbrv);
dlCovs.put("dcIssuedt",dcIssuedt);
dlCovs.put("vecatg",vecatg);
输出类似于:
{
"covabbrv" : ["MCWG", "3WTR", "PSV BUS", "LMV", "INVCRG"],
"dcIssuedt" : ["NA", "NA", "NA", "NA", "NA"],
"vecatg" : ["MCWG", "3WTR", "PSV BUS", "LMV", "INVCRG"]
}
答案 1 :(得分:0)
使用dlCovs
作为JSONArray,我会做这样的事情:
JSONArray dlCovs = new JSONArray();
dlcov = jsonObject.getJSONObject("result").getJSONObject("cov_details");
Iterator<String> x = dlcov.keys();
while (x.hasNext()) {
String key1 = x.next();
String value1 = dlcov.optString(key1);
JSONObject currentDlCov = new JSONObject();
currentDlCov.put("covabbrv",key1);
currentDlCov.put("dcIssuedt",value1);
currentDlCov.put("vecatg",key1);
dlCovs.add(currentDlCov);
}
dlData.put("dlCovs", dlCovs);
如果你的dlCovs
JSONObject,这将添加一个JSONObjects数组作为dlData
的值。