2016-11-13 23 views
-4
{ 
    "status": 200, 
    "message": "API executed successfully.", 
    "data": [{ 
     "data": [{ 
      "shopToken": "NQ2", 
      "Id": 5, 
      "UserId": 5, 
      "ShopName": "test", 
      "ContactName": "test", 
      "ContactNumber": "test", 
      "Address": null, 
      "IsActive": true, 
      "OwnerName": "Test" 
     }], 
     "recordsFiltered": 1, 
     "recordsTotal": 1, 
     "draw": 0, 
     "pageIndex": 0, 
     "pageSize": 1 
    }] 
} 

このJSONをAndroidで解析するにはどうすればよいですか?jsonオブジェクト内のJson配列とオブジェクト

@Override 
protected String doInBackground(Void... voids) { 
    OkHttpClient client = new OkHttpClient.Builder().connectTimeout(5, TimeUnit.MINUTES) 
     .readTimeout(5, TimeUnit.MINUTES) 
     .build(); 

    Request request = new Request.Builder() 
     .url(Config.GLOPOSNET_URL+"getShops?userToken=NQ2") 
     .build(); 

    try { 
     Response response = client.newCall(request).execute(); 
     String res = response.body().string(); 

       try { 
        JSONObject joa = new JSONObject(res); 
        int status = joa.getInt("status"); 
        msg = joa.getString("message"); 
        System.out.println("status : "+status); 

        if (status == 200){ 
         JSONArray infoa = joa.getJSONArray("data"); 
         for (int i=0; i<=infoa.length(); i++){ 
          JSONObject johaa = infoa.getJSONObject(i); 
          System.out.println("object=== "+johaa.toString()); 

          ModelClassShopList pstShopList = new ModelClassShopList(); 
          pstShopList.getShopToken(); 
          pstShopList.getId(johaa.getInt("Id")); 
          pstShopList.getUserId(johaa.getString("UserId")); 
          pstShopList.getShopName(johaa.getString("ShopName")); 
          pstShopList.getContactName(johaa.getString("ContactName")); 
          pstShopList.getContactNumber(johaa.getString("ContactNumber")); 
          pstShopList.getAddress(johaa.getString("Address")); 
          pstShopList.getOwnerName(johaa.getString("OwnerName")); 

          String shop_token = pstShopList.getShopToken(); 
          String user_name = pstShopList.getShopName(johaa.getString("UserId")); 
          String user_type = pstShopList.getContactName(johaa.getString("UserId")); 
          String user_addres = pstShopList.getContactNumber(johaa.getString("UserId")); 


          System.out.println("shop token=== "+shop_token); 

          SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE); 
          SharedPreferences.Editor editor = pref.edit(); 
          editor.putString(Config.SHOP_TOKEN, shop_token); 
          editor.commit(); 
         } 
        }else { 
         AlertDialog.Builder builder; 
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
          builder = new AlertDialog.Builder(MainActivity.this, android.R.style.Theme_Material_Light_Dialog_Alert); 
          builder.setMessage(""+msg); 
          builder.setCancelable(false); 
          builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 

           @Override 
           public void onClick(DialogInterface dialog, int which) { 
            dialog.dismiss(); 
           } 
          }); 
          builder.show(); 
         } 
        } 


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

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

      return null; 
     } 
+0

あなたの答えを得るdidntは?どこからphp、javaを渡しているのですか? – siddhesh

+0

まだありません。私はアンドロイドから構文解析しています。 –

+0

は、データ配列オブジェクトをデータの中に取り込むのにまだ混乱しています。 –

答えて

0

ジャクソンライブラリを使用してjsonを解析できます。

例: -

class Student { 
    private String name; 
    private int age; 

    public Student(){} 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public int getAge() { 
     return age; 
    } 

    public void setAge(int age) { 
     this.age = age; 
    } 
    public String toString(){ 
     return "Student [ name: "+name+", age: "+ age+ " ]"; 
    } 
} 


public static void main(String args[]){ 

     ObjectMapper mapper = new ObjectMapper(); 
     String jsonString = "{\"name\":\"Mahesh\", \"age\":21}"; 

     //map json to student 

     try{ 
     Student student = mapper.readValue(jsonString, Student.class); 

     System.out.println(student); 

     mapper.enable(SerializationConfig.Feature.INDENT_OUTPUT); 
     jsonString = mapper.writeValueAsString(student); 

     System.out.println(jsonString); 
     } 
     catch (JsonParseException e) { e.printStackTrace();} 
     catch (JsonMappingException e) { e.printStackTrace(); } 
     catch (IOException e) { e.printStackTrace(); } 
    } 

参照してください:http://www.tutorialspoint.com/jackson/

http://www.journaldev.com/2324/jackson-json-java-parser-api-example-tutorial

+0

私はokhttpライブラリを使って試しています..もしあなたがそれについて考えているなら、plsと共有してください。 –

+0

あなたはこれを見ましたか?http://www.vogella.com/tutorials/JavaLibrary-OkHttp/article.html –

+0

私Jaksonは人気のあるライブラリでシンプルなツールだと思います。あなたの仕事のためにそれを使用してください。 –

0

Gsonユーティリティを使用して、任意のjsonを解析できます。

Map<String, List<ErrorInfo>> map = new HashMap<>(); 
    map.put("ErrorInfos", list); 
    Gson gson = new Gson(); 
    String json = gson.toJson(map); 
    Log.i("response", json); 

そして、私のJSON strがある:

{"ErrorInfos":[{"errorDetails":"Connect time out"},{"errorDetails":"Connect time out"}]}

2

最初の1つのデータ内データを得るためにあなたのJSON文字列

JSONObject jObject = new JSONObject(youStringData); 
    JSONArray jArray = jObject.getJSONArray("data"); 

からデータ配列を取得するには

for(int i = 0; i < jArray.length(); i++){ 
    JSONArray dataTwo = jArray.getJSONObject(i).getJSONArray("data");; 
} 

あなたが主な目的で他の値を取得したい場合は、

jObject.getInt("status") 
+0

これは近いです。私は最初のデータ配列内のデータ配列について質問していると思います。可能であれば –

+0

私はデータの中に別の "データ"を取得する考えを与える。 –

+0

OK、私は私の答えを更新する –

2

はこれを試してみてください使用することができ、

// Create JSON object 
JSONObject jObject = new JSONObject(res); 

// Get the outer "data" array 
JSONArray dataOuter = jObject.getJSONArray("data"); 

// Iterate the outer "data" array 
for (int i = 0; i < dataOuter.length() ; i++) { 
    JSONObject jObject1 = dataOuter.getJSONObject(i); 

    // Get the inner "data" array 
    JSONArray dataInner = jObject1.getJSONArray("data"); 

    // Iterate the inner "data" array 
    for (int j = 0; j < dataInner.length() ; j++) { 

     JSONObject jItem = dataInner.getJSONObject(i); 
     String shopToken = jItem.getString("shopToken"); 
     // parse the rest of the items 

    } 
} 
+2

ありがとう@k neeraj lal ....私はあなたの助けを借りて(y)を助けて私の質問を解決しました –

+0

@BhavyaGusai –

関連する問題