处理来自服务器android的数组数据

时间:2018-04-24 06:49:41

标签: java android json

在我的应用程序数据中,来自服务器的数组形式为enter image description here

我无法处理我将分享我的代码的数据,请帮助我。

 JSONObject jsonObject = new JSONObject(response);
  String name = jsonObject.getString("status");
                    String name1 = name.trim();
 if (name1.equals("success")) {

                        Toast.makeText(getApplicationContext(),"inside",Toast.LENGTH_LONG).show();
                        try {


                            JSONArray array = jsonObject.getJSONArray("data");

                            for (int i = 0; i < array.length(); i++) {


                                jsonObject = array.getJSONObject(i);


                                s_key = jsonObject.getString("initKey");
                                s_iv = jsonObject.getString("initIv");
                                sec_url = jsonObject.getString("url");
                                s_init_hash = jsonObject.getString("initHash");


                            }
                        } catch (JSONException e) {
                            e.printStackTrace();


        }

6 个答案:

答案 0 :(得分:1)

没有JSONArray

 JSONObject jsonObject = new JSONObject(response);
      String name = jsonObject.getString("status");
                        String name1 = name.trim();
     if (name1.equals("success")) {

                            Toast.makeText(getApplicationContext(),"inside",Toast.LENGTH_LONG).show();
                            try {
                                   JSONObject  jsonObjectData = jsonObject .getJSONObject(i);
                                    s_key = jsonObjectData.getString("initKey");
                                    s_iv = jsonObjectData.getString("initIv");
                                    sec_url = jsonObjectData.getString("url");
                                    s_init_hash = jsonObjectData.getString("initHash");
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();


            }

答案 1 :(得分:0)

您的回复是JSONObject而不是JSONArray检查

<强> FYI

{ } 括号表示JSONObject

[ ] 括号表示JSONArray

解析你的json

    JSONObject jsonObject = new JSONObject(response);
    String name = jsonObject.getString("status");
    String name1 = name.trim();
    if (name1.equals("success")) {

        Toast.makeText(getApplicationContext(), "inside", Toast.LENGTH_LONG).show();
        try {


            JSONObject jsonObjectData = jsonObject.getJSONObject("data");
            s_key = data.getString("initKey");
            s_iv = data.getString("initIv");
            sec_url = data.getString("url");
            s_init_hash = data.getString("initHash");


        } catch (JSONException e) {
            e.printStackTrace();


        }


    }

答案 2 :(得分:0)

数据不是Json数组导致它以{}它的Json对象

开始

Json Array以[]

开头

所以你需要使用

SONObject  jsonObjectData = jsonObject .getJSONObject(i);

答案 3 :(得分:0)

当您在Android中使用JSON数据时,您将使用JSONArray来解析以数组括号开头的JSON。 JSON中的数组用于组织相关项的集合(可以是JSON个对象)。 例如:

  

[{&#34; name&#34;:&#34; item 1&#34;},{&#34; name&#34;:&#34; item2}]

另一方面,在处理以花括号开头的JSONObject时,您会使用JSONJSON对象通常用于包含与一个项目相关的键/值对。例如:

  

{&#34; name&#34;:&#34; item1&#34;,&#34; description&#34;:&#34; JSON对象&#34;}

 JSONObject jsonObject = new JSONObject(response);
  String name = jsonObject.getString("status");
                    String name1 = name.trim();
 if (name1.equals("success")) {

                        Toast.makeText(getApplicationContext(),"inside",Toast.LENGTH_LONG).show();
                        try {
                              JSONObject data = jsonObject. getJSONObject("data");
        s_key = data.getString("initKey");
        s_iv = data.getString("initIv");
        sec_url = data.getString("url");
        s_init_hash = data.getString("initHash");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();


        }

答案 4 :(得分:0)

 JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(response);
                    String name = jsonObject.getString("status");
                    String name1 = name.trim();
                    if (name1.equals("success")) {



                        JSONObject jsonObjectData = jsonObject.getJSONObject("data");
                        s_key = jsonObjectData.getString("initKey");
                        s_iv = jsonObjectData.getString("initIv");
                        sec_url = jsonObjectData.getString("url");
                        s_init_hash = jsonObjectData.getString("initHash");





                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

答案 5 :(得分:0)

<强>响应 {     &#34;状态&#34;:&#34;成功&#34;,     &#34;数据&#34;:{

    "initKey": "abc",
    "initHash": "cde",
    "initIv": "efg",
    "versionNo": "123 ",
    "url": "https://www.youtube.com"


}

}

<强>代码

 try {

            JSONObject outerJsonObject=new JSONObject(response);
            String status=outerJsonObject.getString("status");
            if(status.equals("success"))
            {

                JSONObject innerJsonObjectData=outerJsonObject.getJSONObject("data");
                String initKey =innerJsonObjectData.getString("initKey");
                String initHash =innerJsonObjectData.getString("initHash");
                String initIv =innerJsonObjectData.getString("initIv");
                String versionNo =innerJsonObjectData.getString("versionNo");
            }

            } catch (JSONException e) {
            e.printStackTrace();
        }

<强>评论

 /*

            {
                //outerJsonObject

                    "status":"success",

                    "data":                // innerJsonObjectData
                    {

                           "initKey":"abc",
                            "initHash": "cde",
                            "initIv": "efg",
                            "versionNo": "123 ",
                            "url": "https://www.youtube.com"

                    }


            }

*/