I have Response
{
"p1":{
"date":"Sat, 29 Jul 2017 03:31:43 GMT",
"plan_id":"5c4c6548-38cb-4dbb-b7a3-a3f260fb8532",
"plan_name":"Ultra50",
"pulse":1,
"tarrif":1,
"validity":30
},
"p2":{
"date":"Sat, 29 Jul 2017 03:31:43 GMT",
"plan_id":"5c4c6548-38cb-4dbb-b7a3-a3f260fb8532",
"plan_name":"Ultra50",
"pulse":1,
"tarrif":1,
"validity":30
}
}
Here my Code
Response.Listener<JSONObject> responseListener = new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject plan=response.getJSONObject("p1");
Iterator x=plan.keys();
JSONArray jsonElements=new JSONArray();
while (x.hasNext()){
String key=(String) x.next();
jsonElements.put(plan.get(key));
Log.e(TAG, "onResponse: "+key );
}
Log.d(TAG, "onResponse: "+jsonElements.toString());
} catch (JSONException e) {
e.printStackTrace();
}
how to convert p1,p2,p3 in one JsonArray?
You should use ITERATOR .
It is used to traverse a collection object elements one by one .
Iterator iteratorObj = response.keys();
while (iteratorObj.hasNext())
{
String getKEY = (String)iteratorObj.next();
System.out.println("Key: " + Key + "------>" + getKEY); // p1,p2
}
String res = response.toString().trim(); res = res.replaceFirst("\\{","]"); res = res.substring(0,res.length()-1) + "]"; try { JSONArray arr = new JSONArray(res.toString()); }catch (JSONException j) { }
Iterator planKeys = response.keys();
{
JSONArray jsonElements=new JSONArray();
while(planKeys.hasNext()){
String key = (String) planKeys.next();
JSONObject plan = response.getJSONObject(key);
Iterator x=plan.keys();
while (x.hasNext()){
String key=(String) x.next();
jsonElements.put(plan.get(key));
Log.e(TAG, "onResponse: "+key );
}
}
}
JSONObject plan=response.getJSONObject("p1");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.