【问题标题】:Decode JSON String in Android在 Android 中解码 JSON 字符串
【发布时间】:2017-02-21 05:42:54
【问题描述】:

我有一个 JSON 格式的联系人。现在我想将它们解码为字符串数组。有两个数组; namesphones。我正在使用此代码:

    String[] names;
    String[] phones;

    String test = "[{\"name\":\"A\",\"phone\":\"911\"},{\"name\":\"A1\",\"phone\":\"911\"},{\"name\":\"Abid\",\"phone\":\"371812\"}]";
    try {
        JSONArray jsonArray = new JSONArray(test);
        JSONObject jsonObject = new JSONObject(jsonArray.toString());
        Log.i("INFO", String.valueOf(jsonObject.length()));
    } catch (JSONException e) {
        e.printStackTrace();
    }

这会产生错误。如何添加names 数组中的所有名称和phones 数组中的所有电话。就像names[0] 被分配了A,这是一个名字,phones[0] 被分配了911,这是与名字相对应的第一个电话号码。我怎么能做到这一点,我是 android 新手?

【问题讨论】:

  • new JSONObject(jsonArray.toString()) 应该达到什么目的?!
  • 我认为数组中有对象所以,我尝试检索这些。好像有三个对象,不是吗?
  • 你必须通过遍历每个对象来获取它,你不能像那样简单地获取它
  • @PakDeveloper 是的,那你为什么要把数组变成 one 对象?!

标签: android arrays json


【解决方案1】:

问题出在这一行

JSONObject jsonObject = new JSONObject(jsonArray.toString());

您正在尝试将 JSONArray 转换为 JSONObject ,但您不能。如果你想在JSONArray 中访问JSONObject,你必须遍历每一个,或者你可以通过它的索引来获取特定的对象。

    String[] names;
    String[] phones;

    String test = "[{\"name\":\"A\",\"phone\":\"911\"},{\"name\":\"A1\",\"phone\":\"911\"},{\"name\":\"Abid\",\"phone\":\"371812\"}]";
    try {
        JSONArray jsonArray = new JSONArray(test);
        phones = names = new String[jsonArray.length()];
        for(int i = 0 ; i < jsonArray.length(); i ++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            names[i] = jsonObject.getString("name");
            phones[i] = jsonObject.getString("phone");
            Log.i("INFO", "name : " + jsonObject.getString("name") + " , phone : " + jsonObject.getString("phone"));

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

【讨论】:

  • 这个答案看起来很完美。
【解决方案2】:

您不能像这样将 json arrya 转换为 json 对象:

try {
        JSONArray jsonArray = new JSONArray(test);
        for(int i=0; i<jsonArray.length(); i++) {
           JSONObject jsonObject = jsonArray.getJSONObject(i);
           // Your code goes here..  
        }

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

【讨论】:

    【解决方案3】:

    这是所需的代码。

     ArrayList<String> names = new ArrayList<>();
        ArrayList<String> phones = new ArrayList<>();
    
        String test = "[{\"name\":\"A\",\"phone\":\"911\"},{\"name\":\"A1\",\"phone\":\"911\"},{\"name\":\"Abid\",\"phone\":\"371812\"}]";
        try {
            JSONArray jsonArray = new JSONArray(test);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String name = jsonObject.getString("name");
                String phone = jsonObject.getString("phone");
                names.add(name);
                phones.add(phone);
                Log.i("INFO", name + ", " + phone);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    

    上面你使用了简单的数组,它们只能存储固定大小的数据。但是arrayList和List可以存储可变大小的数据,或者你不需要固定它们的大小。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-19
      • 1970-01-01
      • 2013-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-11
      相关资源
      最近更新 更多