如何将json数组字符串转换为json对象

时间:2016-09-03 14:29:00

标签: java android json

[{"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"}]   

需要将此Array字符串转换为java中的JSONObject

这是请求代码,来自服务器的响应很好,但它是字符串类型,如何将此响应字符串转换为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 个答案:

答案 0 :(得分:1)

您需要先将其转换为JSONArray,因为JSON架构位于JSONArray中。

JSONArray jsonArrayResult = new JSONArray(response);

现在使用从这个jsonArrayResult获取JSONObject。

for(int i =0; i<jsonArrayResult.length();i++)
{
JSONObject jsonResult =jsonArrayResult.get(i);
// Do what ever you want to do with jsonResult.
}

答案 1 :(得分:0)

就这么简单:

    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"));