0

需要帮助在 Android 中解析这个 json。解析必须通过 URL 获取。 http://www.azmetr.com/view.json 得到了很多教程。但是无法学习如何解析这种基础。

我使用了原生 JSONObject。还是使用 GSON 或其他更好?

{
   "azmetr":[
      {
         "mebel":{
            "nid":"39",
            "title":"Oturacaq 4"
         }
      },
      {
         "mebel":{
            "nid":"38",
            "title":"Oturacaq 3"
         }
      },
      {
         "mebel":{
            "nid":"37",
            "title":"Oturacaq 2"
         }
      },
      {
         "mebel":{
            "nid":"36",
            "title":"Oturacaq 1"
         }
      },
      {
         "mebel":{
            "nid":"35",
            "title":"Ag kreslo"
         }
      },
      {
         "mebel":{
            "nid":"34",
            "title":"Ag divan"
         }
      },
      {
         "mebel":{
            "nid":"33",
            "title":"Zebra kreslo"
         }
      },
      {
         "mebel":{
            "nid":"32",
            "title":"Zebra divan"
         }
      },
      {
         "mebel":{
            "nid":"31",
            "title":"Korolevski kreslo"
         }
      },
      {
         "mebel":{
            "nid":"30",
            "title":"Korolevski divan"
         }
      }
   ]
}
4

2 回答 2

0

尝试使用此代码可以解析 json 响应数组。此代码对我有用。

public void update(){

         try {
                HttpParams httpParams = new BasicHttpParams();
                HttpConnectionParams.setConnectionTimeout(httpParams, 0);
                HttpConnectionParams.setSoTimeout(httpParams, 0);
                //
                HttpParams p = new BasicHttpParams();
                p.setParameter("user", "1");

                // Instantiate an HttpClient
                HttpClient httpclient = new DefaultHttpClient(p);                   
                String url = "Your URL";
                Log.i(url, "send  task - start webservice");

                HttpPost httppost = new HttpPost(url);
                // Instantiate a GET HTTP method
                try {
                    Log.i(getClass().getSimpleName(), "send  task - start");
                    //
                    ResponseHandler<String> responseHandler = new BasicResponseHandler();
                    String responseBody = httpclient.execute(httppost,  responseHandler);
                    // Parse
                    JSONObject json = new JSONObject(responseBody);
                    JSONArray jArray = json.getJSONArray("posts");
                    int count = jArray.length();
                    mebel= new String[count];
                    nid= new String[count];
                    title = new String[count];



                    for (int i = 0; i < jArray.length(); i++) {
                        JSONObject e = jArray.getJSONObject(i);
                        String s = e.getString("post");
                        JSONObject jObject = new JSONObject(s);

                        mebel[i]    =   jObject.getString("mebel");
                        nid[i]  =   jObject.getString("nid");
                        title[i]    =   jObject.getString("title");

                    }


                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } 
         catch (Throwable t) {
               Toast.makeText(HomeActivity.this, "Request failed: " + t.toString(),Toast.LENGTH_LONG).show();
         }  


    }

注意:如果您使用的是 2.3 版本或 api 级别 10,则需要此代码并将其放在 onCreate 之后。

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
于 2013-06-24T01:57:38.177 回答
0

嘿,没有人可以教SO All the people is ready to solving problem not teaching

如果您遇到任何问题,请查看示例并了解自己,然后发表评论。

于 2013-06-24T04:36:22.640 回答