getting Json result in Android
Json Problem :
I have this formatted JSON
{
"Table1":[
{
"TableName":"LoadDistributor",
"Description":"Distributor ",
"MandatoryFlag":"1",
"Status":"",
"Priority":"0"
},
{
"TableName":"LoadPrice",
"Description":"Price ",
"MandatoryFlag":"1",
"Status":"",
"Priority":"0"开发者_Python百科
},
{
"TableName":"LoadProduct",
"Description":"Product ",
"MandatoryFlag":"1",
"Status":"",
"Priority":"0"
},
{
"TableName":"RD.AlternativeProductDetail",
"Description":"AltProdutDetail",
"MandatoryFlag":"0",
"Status":"",
"Priority":"0"
},
{
"TableName":"XA.vwTown",
"Description":"Town ",
"MandatoryFlag":"1",
"Status":"",
"Priority":"0"
}
] }
Android Json Processing method is:
public String[] getStringArrayResponseJson(SoapPrimitive node, ArrayList<String> strings) {
try{
String result = node.toString();
JSONObject json = new JSONObject(result);
// Parsing
JSONArray nameArray = json.names();
JSONArray valArray = json.toJSONArray(nameArray);
Log.i("@@@" , "===="+ valArray.length());
for (int i = 0; i < valArray.length(); i++) {
strings.add(valArray.getString(i));
}
json.put("sample key", "sample value");
Log.i("REST", "<jsonobject>\n" + json.toString()
+ "\n</jsonobject>");
}catch (JSONException e) {
e.printStackTrace();
}
My requirement is I want to get the TableName into string[] or list... How we can do this?
Please help me what is the error in my json processing method:?
Example code, not sure that its going to work. I create an ArrayList for the strings just for the sake of it.
ArrayList<String> list = new ArrayList<String>();
JSONArray array = jsonobject.getJSONArray("Table1");
int max = array.length();
for (int i = 0; i < max; i++) {
JSONObject tmp = array.getJSONObject(i);
list.add(tmp.getString("TableName"));
}
Might be a quicker way but just for an example.
You could read more http://www.androidcompetencycenter.com/2009/10/json-parsing-in-android/
精彩评论