Android如何:获取JSONObject和JSONArray

时间:2019-05-16 10:48:02

标签: android arrays json parse-platform

我无法从json文件中获取JSONArray和JSONObject。

这就是我输入数组和对象的方式。

query1.getFirstInBackground(new GetCallback<ParseObject>() {
                @Override
                public void done(ParseObject object, ParseException e) {
                    if (e == null) {
                        final JSONObject myObject = new JSONObject();
                        try {
                            myObject.put("Preço", gymClassPrice.getText().toString());
                            myObject.put("Aula", gymClass.getText().toString());
                            myObject.put("Horario", gymClassSchedule.getText().toString());
                        } catch (JSONException e1) {
                            e1.printStackTrace();
                        }
                        myArray.put(myObject);
                        gyms.put("Classes", myArray);
                        gyms.saveInBackground();
                    } else {

                        gyms.put("Name", gymName.getText().toString());
                        gyms.put("Place", gymPlace.getText().toString());
                        gyms.put("Adress", gymLocation.getText().toString());
                        gyms.put("Schedule", gymSchedule.getText().toString());
                        gyms.put("Description", gymDescription.getText().toString());

                        try {
                            myObject.put("Preço", gymClassPrice.getText().toString());
                            myObject.put("Aula", gymClass.getText().toString());
                            myObject.put("Horario", gymClassSchedule.getText().toString());
                        } catch (JSONException e1) {
                            e1.printStackTrace();
                        }
                        myArray.put(myObject);
                        gyms.put("Classes", myArray);
                        gyms.saveInBackground();

                    }
                }

现在我不知道如何获得这些价值。 有人可以帮我吗?

这是我在json文件中的实际数组:

 "Classes": [
  {
    "Horario": "gababa",
    "Preço": "sgaffa",
    "Aula": "vsagav"
  },
  {
    "Horario": "gababa566",
    "Preço": "sgaffa566",
    "Aula": "vsagav345"
  }
],

2 个答案:

答案 0 :(得分:0)

您可以在Android中为获取json对象和json数组创建这2个函数

public JSONObject getJSONObject(String name) throws JSONException {
    Object object = get(name);
    if (object instanceof JSONObject) {
        return (JSONObject) object;
    } else {
        throw JSON.typeMismatch(name, object, "JSONObject");
    }
}

第二个是获取json数组

public JSONArray getJSONArray(String name) throws JSONException {
    Object object = get(name);
    if (object instanceof JSONArray) {
        return (JSONArray) object;
    } else {
        throw JSON.typeMismatch(name, object, "JSONArray");
    }
}

现在您可以像这样访问

try {
    JSONArray badgesJsonArray = dataJsonObject.getJSONArray("Classes");
    if (Utility.validateJsonArray(badgesJsonArray)) {
        for (int i = 0; i < badgesJsonArray.length(); i++) {
                try {
                  JSONObject badgesJsonObject = badgesJsonArray.getJSONObject(i);
                    }catch (Exception e) {
                    }
             }

} catch (Exception e) {
}

答案 1 :(得分:0)

  

尝试此代码

     

首先,您需要提取json数组类,然后通过jsonArray循环到   在获取JSONObject所需的值后,获取JSONObject

try {
            JSONArray array = object.getJSONArray("Classes");
            for (int i = 0; i <array.length() ; i++) {

                try {
                    JSONObject jsonObject = array.getJSONObject(i);
                    // to get value of Horario

                    String Horario = jsonObject.getString("Horario");

                    // to get value of Preço

                    String Preço = jsonObject.getString("Preço");

                    // to get value of Aula

                    String Aula = jsonObject.getString("Aula");
                }catch (JSONException e){

                }

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

希望这会有所帮助