Android Json result processing
I have Json result like this: array.getJSONObject(j) --
{"ExecutiveCode":"WAT2 ","FreeIssuePrefix":"
","DisPaySchedulePrefix":"","NextFreeIssueNo":"1","NextReturnNo":"20","UploadedType":"1","DisNextFreeIssueNo":"1","DisNextFOCNo":"1","NextVisitNo":"15","DisNextOrderNo":"1","UploadedOn":"Jun 17 2011 6:33PM","NextReturnAcceptNo":"1","BusinessUnit":"HEMA","TXNReferencePrefix":"20110708 ","OrderPrefix":"OR4 ","UploadedMethod":"3","FOCPrefix":" ","ReturnPrefix":"RT4 ","RetailerPrefix":"TEM4","NextRetailerNo":"10","NextInvoiceNo":"1","NextGRNNo":"1","InvoicePrefix":"IN4 ","NextTXNReference":"2","NextOrderNo":"37","ReturnAcceptPrefix":" ","PaySchedulePrefix":"PS4","NextReceiptNo":"1","NextFOCNo":"20","NextPayScheduleNo":"41","NextGRONo":"1","DisReturnPrefix":" ","DisReceiptPrefix":" ","DisNextReturnNo":"1","DisOrderPrefix":" ","DisNextReceiptNo":"1","DisNextPayScheduleNo":"1","NextActivityNo":"1","DisInvoicePrefix":" ","DisNextInvoiceNo":"1","UploadedBy":"WAT2","DisFreeIssuePrefix":" ","DisFOCPrefix":" ","ReceiptPrefix":"RP4 "}
And I have take vaue & name in the list:
I have written like this :
try {
// getSoapResponseTableDataJson(responsePrimitiveData,
// null,tablesName.get(i));
String result = responsePrimitiveData.toString();
JSONObject jsonobject = new JSONObject(result);
ArrayList<String> toFieldList = new ArrayList<String>();
ArrayList<String> toFieldValList = new ArrayList<String>();
JSONArray array = jsonobject.getJSONArray("Table1");
int max = array.length();
HashMap<String, String> applicationSettings = new HashMap<String, String>();
for (int j = 0; j < max; j++) {
System.out.println(" -- array.getJSONObject(j) -- "
+ array.getJSONObject(j));
String value = array.getJSONObject(j).getString("value");
String name = array.getJSONObject(j).getString("name");
applicationSettings.put(name, value);
}
System.out.println(" ---- json --- "+ applicationSettings);
开发者_如何学编程 } catch (JSONException e) {
e.printStackTrace();
}
This is saying : org.json.JSONException : No value for value
what is name & value?
Actually I want to get the Key separate list & value pair in separate list....
I want to take like this
name like : {ExecutiveCode,FreeIssuePrefix,DisPaySchedulePrefix,.....}
value like : {WAT2, "","TEst",.....}
Please help me.
Thanks in advance....
just think ... there is no {"name": "something"}
in your JSON ...
EDIT ... i read wrong JSONObject documentation ... there is no JSONObject.getNames()
use obj.names()
JSONObject obj = array.getJSONObject(j);
JSONArray[] names = obj.names();
iterate names
String name = names.getString(i);
String value = obj.getString(name);
applicationSettings.put(name, value)
old answer was:
use:
JSONObject obj = array.getJSONObject(j);
String[] names = JSONObject.getNames(obj);
iterate names
value = obj.getString(names[i])
Use JSON parser libraries, it will save you tonnes of time.
- GSON
- Jackson
Example: Parsing JSON in Jackson here
精彩评论