-1

如何在Android中解析 JSON表单URL

例如:

我单击一个按钮并从服务器下载数据,然后将数据显示在屏幕上。

JSON 代码:

[{"id":1, "name":"ABC"},{"id":2,"name":"ABC"}]

或者

{"response":[{"id":1,"name":"ABC","surname":"ABC"}]}

4

4 回答 4

1
private void Get_List() {
            // TODO Auto-generated method stub
CountDownTimer count_Timer;
            try {
                count_Timer.start();
                SetUrl url = new SetUrl();
                URL myurl = new URL(//"Your URL");
                HttpURLConnection connection = (HttpURLConnection) myurl
                        .openConnection();
                connection.connect();
                int code = connection.getResponseCode();

                if (code != 200) {
                    setContentView(R.layout.webimg);
                    TextView tv_nointernet = (TextView) findViewById(R.id.tv_nointernet);
                    tv_nointernet.setText("Please turn your internet on. . .");
                } else {

                    BufferedReader dis = new BufferedReader(
                            new InputStreamReader(connection.getInputStream()));

                    String myString, Result = dis.readLine();

                    while ((myString = dis.readLine()) != null) {
                        Result += myString;
                    }
                    JSONObject jsonObject = new JSONObject(Result);
                    JSONArray jsonArray = jsonObject.getJSONArray("posts");

                    Rest_List = new ArrayList<Rest_Listing>();

                    for (int i = 0; i < jsonArray.length(); i++) {
                        Rest_Listing R_List = new Rest_Listing();

                        JSONObject O = jsonArray.getJSONObject(i);

                        R_List.setRest_Address1(O.getJSONObject("post")
                                .getString("Name"));


                        Rest_List.add(i, R_List);
                    }
                    count_Timer.cancel();
                }
            } catch (MalformedURLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
于 2014-11-09T05:03:02.527 回答
1

要从 url 获取数据,您可以使用 android-async-http-1.4.1.jar (您可以从http://loopj.com/android-async-http/下载它),并将其放在 libs 文件夹中,然后使用如果json结构像这样“{“response”:[{“id”:1,“name”:“ABC”,“surname”:“ABC”}]}”,则以下代码:

AsyncHttpClient 客户端 = new AsyncHttpClient();

    client.post(Your_URL_String, new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(String MyResponse) {
            JSONObject json_main;
            try {
                JSONObject json_main = new JSONObject(MyResponse);  
                JSONArray json_arr = json_main.getJSONArray("response"); 
                        for(int i = 0; i < json_arr.length(); i++)
                        {
                    JSONObject c = json_arr.getJSONObject(i);
                    String ID = c.getString("id");
                    String name = c.getString("name");
                    String surname = c.getString("surname");
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        @Override
        public void onFailure(Throwable arg0, String arg1) {
            // TODO Auto-generated method stub
            super.onFailure(arg0, arg1);

        }
    });
于 2013-08-19T10:35:03.920 回答
0
Use the following it will be helpful

public void JSONPARSE()
   {
    JSONObject jObj=null;
    JSONArray Jarray=null;
    InputStream is=null;
    String URL ="url";
    System.out.println("verify url is----> "+URL);
    //Making HTTP request
    try {
        // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(URL);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();           
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            String result = sb.toString();
            System.out.println("json RegistrationResult is----> "+result);
            try {
                    jObj = new JSONObject(result);
                } catch (JSONException e) {
                    Log.e("JSON Parser", "Error parsing data " + e.toString());
                }
            try {
                        Jarray = jObj.getJSONArray("response");
                        for(int i = 0; i < Jarray.length(); i++)
                        {
                            JSONObject c = Jarray.getJSONObject(i);
                            String id = c.getString("id");
                            x.add(id);    //x is a vector
                           String name =c.getString("name");
                           n.add(name); //n is a vector
                          String surname =c.getString("surname");
                           s.add(surname); //s is a vector
                        }
                 } catch (JSONException e) {
                     e.printStackTrace();
                 }

        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
    }
于 2013-08-19T10:11:55.533 回答
0

有很多方法可以做到这一点。我发现这个库非常有用且节省时间:Gson Library

您可以在此处访问用户指南: Gson 用户指南

于 2013-08-19T10:14:43.660 回答