can anyone help,I cant seem to fix the erros while trying to parse this API:
{
"createDate": 1321834118923,
"result": "SUCCESS",
"searchTerm": "N7",
"searchableLocations": [
{
"identifier": "OUTCODE^1685",
"name": "N7"
}
]
}
Please help me identify the problem
package com.somcollection;
import android.app.Activity;
import android.os.Bundle;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
public class JsonParser extends Activity {
private JSONObject jObject;
private String jString = "{\"searchTerm\":\"N7\",\"searchableLocations
\":[{\"identifier\":\"OUTCODE^1685\",\"name\":\"N7\"}],\"createDate
\":1321834118923,\"result\":\"SUCCESS\"}";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
parse();
} catch (Exception e) {
e.printStackTrace();
}
}
private void parse() throws Exception {
jObject = new JSONObject(jString);
JSONObject jObject = new JSONObject(jString);
String menuObject = jObject.getString("searchTerm");
JSONArray menuitemArray = jObject.getJSONArray("searchableLocations");
for (int i = 0; i < menuitemArray.length(); i++) {
JSONObject jsonObj = menuitemArray.getJSONObject(i);
String attributeId = jsonObj.getString("identifier");
System.out.println(attributeId);
String attributeValue = jsonObj.getString("name");
System.out.println(attributeValue);
}
String createDate = jObject.getString("jObject");
String result = jObject.getString("result");
}
}
The following line will fail:
Since you have no key in your JSON named “jObject”. I would expect you would need to write
As a side note, you may not want to use
getString()for this value either since technically it is defined in the JSON as an integer.