【问题标题】:JSON parsing to List view with multiple objects Android [closed]JSON解析到具有多个对象Android的列表视图[关闭]
【发布时间】:2016-07-02 19:09:15
【问题描述】:

需要帮助

我有 Json,我必须通过它检索对象但无法检索。 我也使用了多个对象来检索但没有成功。 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

我的 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();}
    }
}

【问题讨论】:

  • 有什么问题?任何堆栈跟踪?
  • 07-03 03:54:08.988 2987-2987/com.example.premi.jsonlist E/dalvikvm:找不到类 'android.os.PersistableBundle',引用自方法 com.example。 premi.jsonlist.MainActivity.access$super

标签: android json


【解决方案1】:

试试这个:

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) {
}

【讨论】:

  • 07-03 04:09:21.584 30798-30798/com.example.premi.jsonlist E/Trace: 打开跟踪文件时出错: 没有这样的文件或目录 (2) 07-03 04:09 :21.616 30798-30798/com.example.premi.jsonlist E/dalvikvm: 找不到类 'android.util.ArrayMap',引用自方法 com.android.tools.fd.runtime.MonkeyPacher.monkeyPatchExistingResources
  • 您似乎正在尝试使用 API 级别的方法,即用于 ArrayMap 的 API 19,您正在测试它的设备不支持该方法。我不熟悉这个问题,但请查看 stackoverflow.com/questions/36812913/…stackoverflow.com/questions/36930200/…
  • 我需要更多帮助,如果我在本地 paasing json 它正在工作,当我使用 api (URL) 时,它给出的错误显示为空白......原因是什么?他们有什么方法可以先在本地存储 json 然后检索?
  • 不确定您的意思,但它需要采用 JSON 字符串格式才能解析。如果我理解正确,您可以将 JSON 字符串存储在 SharedPreferences 中以便稍后检索它。
【解决方案2】:

完成了,我试过了

   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();}
}

我刚刚删除了状态对象

还有一个 for 循环,感谢您的帮助,得到您的一点帮助:)

【讨论】:

  • 没问题,很高兴你知道了! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多