[{"ID":"1",
"Profileimg":"http:\/\/192.168.0.104\/JSON\/jsontestpics\/mr-robot-wallpaper-3.jpg",
"Heading":"Heading Test 1",
"Timestamp":"2016-08-28 11:06:00",
"Tag":"ACM",
"Content":"contentttttFifty-seven tornadoesFifty-seven tornadoesFifty-seven tornadoesFifty-seven",
"Contentimg":"http:\/\/192.168.0.104\/JSON\/jsontestpics\/mr-robot-wallpaper-3.jpg"}]
Need to convert this Array string into JSONObject in java
需要将此Array字符串转换为java中的JSONObject
THIS IS THE REQUEST CODE, response from the server is fine but it is string type,how to convert this response string into json object.
这是请求代码,来自服务器的响应很好,但它是字符串类型,如何将此响应字符串转换为json对象。
StringRequest stringRequest = new StringRequest(Request.Method.POST, Home_posts_config.DATA_URL_1,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//Dismissing progress dialog
loading.dismiss();
Log.d("asdfas","response-__"+response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("asdfas","response_errorrrr"+error.getMessage());
Toast.makeText(getApplicationContext(), "ERRORR", Toast.LENGTH_SHORT).show();
loading.dismiss();
}
}){
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("heading", hdtxt);
params.put("tag",tgtxt);
params.put("fetchid",idd);
return params;
}
};
//Creating request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(stringRequest);
}
2 个解决方案
#1
1
You need to first convert it into JSONArray as you get JSON schema is in JSONArray.
您需要先将其转换为JSONArray,因为JSON架构在JSONArray中。
JSONArray jsonArrayResult = new JSONArray(response);
Now use you get JSONObject from this jsonArrayResult.
现在使用从这个jsonArrayResult获取JSONObject。
for(int i =0; i<jsonArrayResult.length();i++)
{
JSONObject jsonResult =jsonArrayResult.get(i);
// Do what ever you want to do with jsonResult.
}
#2
0
As simple as that:
就如此容易:
String response = "[{\"ID\":\"1\", \"Profileimg\":\"http://192.168.0.104/JSON/jsontestpics/mr-robot-wallpaper-3.jpg\", \"Heading\":\"Heading Test 1\", \"Timestamp\":\"2016-08-28 11:06:00\", \"Tag\":\"ACM\", \"Content\":\"contentttttFifty-seven tornadoesFifty-seven tornadoesFifty-seven tornadoesFifty-seven\", \"Contentimg\":\"http://192.168.0.104/JSON/jsontestpics/mr-robot-wallpaper-3.jpg\"}]";
JSONObject array = new JSONObject("{'data':"+response+"}");
System.out.println(array.get("data"));
#1
1
You need to first convert it into JSONArray as you get JSON schema is in JSONArray.
您需要先将其转换为JSONArray,因为JSON架构在JSONArray中。
JSONArray jsonArrayResult = new JSONArray(response);
Now use you get JSONObject from this jsonArrayResult.
现在使用从这个jsonArrayResult获取JSONObject。
for(int i =0; i<jsonArrayResult.length();i++)
{
JSONObject jsonResult =jsonArrayResult.get(i);
// Do what ever you want to do with jsonResult.
}
#2
0
As simple as that:
就如此容易:
String response = "[{\"ID\":\"1\", \"Profileimg\":\"http://192.168.0.104/JSON/jsontestpics/mr-robot-wallpaper-3.jpg\", \"Heading\":\"Heading Test 1\", \"Timestamp\":\"2016-08-28 11:06:00\", \"Tag\":\"ACM\", \"Content\":\"contentttttFifty-seven tornadoesFifty-seven tornadoesFifty-seven tornadoesFifty-seven\", \"Contentimg\":\"http://192.168.0.104/JSON/jsontestpics/mr-robot-wallpaper-3.jpg\"}]";
JSONObject array = new JSONObject("{'data':"+response+"}");
System.out.println(array.get("data"));