2016-07-02 9 views
1

ヘルプが必要複数のオブジェクトを含むリストビューへのJSON解析Android

私はオブジェクトを取得する必要がありますが、取得することはできません。 私は複数のオブジェクトを使用して検索しましたが、成功しませんでした。 JSON:

{ 
    "status": 1, 
    "data": [{ 
     "restaurent_id": "1", 
     "user_id": "6", 
     "zone_id": "1", 
     "restaurentAddress": { 
      "restaurent_address_id": "1" 
     }, 
     "restaurentInfo": { 
      "restaurent_info_id": "1", 
      "restaurent_bussiness_owner_name": "Vijay" 
     }, 
     "restaurentSetting": { 
      "restaurent_setting_id": "1", 
      "minimum_purcase": "200", 
      "payment_method_id": "1", 
      "title": "Best Hotel" 
     }, 
     "zone": { 
      "zone_id": "1", 
      "by_zipcode": "1" 
     } 

    }] 
} 

と私はrestaurentAddressとrestaurentInfoを取得したい

MY mainActivity.javaファイル

package com.example.premi.jsonlist; 
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

public class MainActivity extends Activity { 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     TextView output = (TextView) findViewById(R.id.textView1); 
     String strJson="{\n" + 
       "\t\"status\": 1,\n" + 
       "\t\"data\": [{\n" + 
       "\t\t\"restaurent_id\": \"1\",\n" + 
       "\t\t\"user_id\": \"6\",\n" + 
       "\t\t\"zone_id\": \"1\",\n" + 
       "\t\t\"restaurentAddress\": {\n" + 
       "\t\t\t\"restaurent_address_id\": \"1\"\n" + 
       "\t\t},\n" + 
       "\t\t\"restaurentInfo\": {\n" + 
       "\t\t\t\"restaurent_info_id\": \"1\",\n" + 
       "\t\t\t\"restaurent_bussiness_owner_name\": \"Vijay\"\n" + 
       "\t\t},\n" + 
       "\t\t\"restaurentSetting\": {\n" + 
       "\t\t\t\"restaurent_setting_id\": \"1\",\n" + 
       "\t\t\t\"minimum_purcase\": \"200\",\n" + 
       "\t\t\t\"payment_method_id\": \"1\",\n" + 
       "\t\t\t\"title\": \"Best Hotel\"\n" + 
       "\t\t},\n" + 
       "\t\t\"zone\": {\n" + 
       "\t\t\t\"zone_id\": \"1\",\n" + 
       "\t\t\t\"by_zipcode\": \"1\"\n" + 
       "\t\t}\n" + 
       "\n" + 
       "\t}]\n" + 
       "}"; 
     String dataoutput = ""; 
     try { 
      JSONObject jsonRootObject = new JSONObject(strJson); 

      //Get the instance of JSONArray that contains JSONObjects 
      JSONObject status = jsonRootObject.optJSONObject("status"); 
      JSONArray Dataarray =status.getJSONArray("data"); 

      //Iterate the jsonArray and print the info of JSONObjects 
      for(int i=0; i < Dataarray.length(); i++){ 
       JSONObject jsonObject = Dataarray.getJSONObject(i); 
JSONObject Data = jsonObject.getJSONObject("restaurentAddress"); 

       for(int j = 0 ; j < Data.length(); j++){ 
        JSONObject GetData =Data.getJSONObject(String.valueOf(j)); 
       int id = Integer.parseInt(GetData.getString("restaurent_address_id")); 
       String postcode = GetData.getString("postcode"); 
       String addresss = GetData.getString("restaurent_address"); 


       dataoutput += " : \n id= "+ id +" \n postcode= "+ postcode +" \n address= "+ addresss +" \n "; 
      }} 
      output.setText(dataoutput); 
     } catch (JSONException e) {e.printStackTrace();} 
    } 
} 
+3

問題は何ですか?任意のスタックトレース? –

+0

07-03 03:54:08.988 2987-2987/com.example.premi.jsonlist E/dalvikvm:メソッドcom.example.premi.jsonlist.MainActivity.accessから参照されるクラス 'android.os.PersistableBundle'が見つかりませんでした$スーパー –

答えて

2

はこれを試してみてください:私はこれを試してみまし完了

try { 
    JSONObject object = (JSONObject) new JSONTokener(YOUR_JSON_STRING).nextValue(); 
    String restaurentAddressId = object.getJSONArray("data").getJSONObject(0).getJSONObject("restaurentAddress").getString("restaurent_address_id"); 
    String restaurentInfoId = object.getJSONArray("data").getJSONObject(1).getJSONObject("restaurentInfo").getString("restaurent_info_id"); 
    String restaurentBizOwnerName = object.getJSONArray("data").getJSONObject(1).getJSONObject("restaurentInfo").getString("restaurent_business_owner_name"); 

} 
catch (JSONException e) { 
} 
+0

07-03 04:09:21.584 30798-30798/com.example.premi.jsonlist E /トレース:開いているトレースファイルのエラー:そのようなファイルまたはディレクトリがありません(2) 07-03 04:09:21.616 30798- 30798 E/dalvikvm/com.example.premi.jsonlist:クラスを見つけることができませんでした「android.util.ArrayMap」、メソッドcom.android.tools.fd.runtime.MonkeyPatcher.monkeyPatchExistingResources –

+0

から参照使用しようとしているように思えテストするデバイスでサポートされていないAPIレベルのAPI 19(ArrayMapのAPI 19)メソッドです。私はこの問題に精通していませんが、http://stackoverflow.com/questions/36812913/dalvikvm-could-not-find-class-androidやhttp://stackoverflow.com/questions/36930200/could-notをチェックしてください-find-class-android-app-applicationonprovideassistdististener –

+0

jsonをローカルで動作させていて、api(URL)として使用しているときに空白を表示している場合は、何か理由がありますか?最初にjsonをローカルに保存してから取得する方法はありますか? –

1

その

try { 
     JSONObject jsonRootObject = new JSONObject(strJson); 

     //Get the instance of JSONArray that contains JSONObjects 
     JSONArray mainnode =jsonRootObject.getJSONArray("data"); 

     //Iterate the jsonArray and print the info of JSONObjects 
     for(int i=0; i < mainnode.length(); i++){ 
      JSONObject jsonObject = mainnode.getJSONObject(i); 
      JSONObject Data = jsonObject.getJSONObject("restaurentInfo"); 
       int id = Integer.parseInt(Data.getString("restaurent_info_id")); 
       String postcode = Data.getString("restaurent_phone_number"); 
       String addresss = Data.getString("restaurent_bussiness_owner_name"); 


       dataoutput += " : \n restaurent_id= "+ id +" \n restaurent_info_id= "+ postcode +" \n restaurent_address_id= "+ addresss +" \n "; 

     } 
     output.setText(dataoutput); 
    } catch (JSONException e) {e.printStackTrace();} 
} 

私だけ削除ステータスオブジェクト

と別のループのために協力いただきありがとうございますが、あなたからの少しのヘルプを手に入れた:)

+0

問題はありません、うれしく思いました! :) –

関連する問題