【问题标题】:I am not able to get the data from a Json array我无法从 Json 数组中获取数据
【发布时间】:2020-04-21 21:05:50
【问题描述】:
Error message:  com.example.myjson W/System.err: org.json.JSONException: Value 
of type org.json.JSONObject cannot be converted to JSONArray 

JSON如下

{

“温度”:296.88,

“feels_like”:298.86,

"temp_min":296.88,

"temp_max":296.88,

“压力”:1013,

“湿度”:89,

“海平面”:1013,

"grnd_level":986

}

我可以单独从中获取数据

String weatherInfo = jsonObject.getString("weather");

不是来自这个字符串为什么?

JSONArray jsonArray1 = new JSONArray(weatherInfo1);
    @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
   
                try {

                    JSONObject jsonObject = new JSONObject(s);

                    String weatherInfo = jsonObject.getString("weather");

                    String weatherInfo1 = jsonObject.getString("main");

                    Log.i("weatherMainContent", weatherInfo1);

                    Log.i("Weather Details" , weatherInfo);


                    JSONArray jsonArray = new JSONArray(weatherInfo);

                    JSONArray jsonArray1 = new JSONArray(weatherInfo1);

                    Log.i("full " , jsonArray1.toString());

                    String message = "";

                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                        String main = jsonObject1.getString("main");
                        String description = jsonObject1.getString("description");
                        Log.i("Weather side Details" , weatherInfo);
                        Log.i("temperaturerrr", jsonObject1.getString("temp_min"));

                        String temp_min = jsonObject1.getString("temp_min");

                        Log.i("temperature", jsonObject1.getString("temp_min"));
                        String pressure = jsonObject1.getString("pressure");


                        if (!main.equals("") && !description.equals("") && !temp_min.equals("")) {

                            message += main + ":" + description +";" + temp_min + "\r\n";
                        } else {
                            Toast.makeText(getApplicationContext(), "couldn't find the giberish you mentioned :(", Toast.LENGTH_SHORT).show();

                        }
                    Log.i("Main", jsonObject1.getString("main"));
                    Log.i("Description", jsonObject1.getString("temp_min"));

                    }

                    if (!message.equals("")) {
                        resultTextView.setText(message);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), "couldn't find the giberish you mentioned :(", Toast.LENGTH_SHORT).show();

                }
            }

    }

【问题讨论】:

  • 它不是一个数组。
  • 您在 JSONObject 上使用 getString,这将为您提供一个原子字符串。然后将 if 传递给 JSONArray 的构造函数,它需要一个 json 数组作为字符串,但事实并非如此。
  • 感谢您的帮助。我是堆栈溢出的新手
  • 您的 JSON 与代码中的属性不匹配。确保您发布了正确的 JSON。
  • @Shivas 你想从 jsonarray 中读取数据吗?

标签: java android json android-studio


【解决方案1】:

你的 Json 不是一个数组,而是一个对象。

为您的 Json 字符串创建一个新的 JSONObject。 然后就直接使用对象,使用getDouble("propertyName")方法获取属性的值:

    String json = "{\"temp\":296.88,\"feels_like\":298.86,\"temp_min\":296.88,\"temp_max\":296.88,\"pressure\":1013,\"humidity\":89,\"sea_level\":1013,\"grnd_level\":986}";

    JSONObject weatherInfo = new JSONObject(json);

    double temp = weatherInfo.getDouble("temp");
    double feels_like = weatherInfo.getDouble("feels_like");
    double temp_min = weatherInfo.getDouble("temp_min");
    double temp_max = weatherInfo.getDouble("temp_max");
    double pressure = weatherInfo.getDouble("pressure");
    double humidity = weatherInfo.getDouble("humidity");
    double sea_level = weatherInfo.getDouble("sea_level");
    double grnd_level = weatherInfo.getDouble("grnd_level");

    System.out.println(temp);
    System.out.println(feels_like);
    System.out.println(temp_min);
    System.out.println(temp_max);
    System.out.println(pressure);
    System.out.println(humidity);
    System.out.println(sea_level);
    System.out.println(grnd_level);

【讨论】:

    【解决方案2】:

    如果这是您的数据 "{"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":300,"main":"Drizzle","description":"light intensity drizzle","icon":"09d"}],"base":"stations","main":{"temp":280.32,"pressure":1012,"humidity":81,"temp_min":279.15,"temp_max":281.15},"visibility":10000,"wind":{"speed":4.1,"deg":80},"clouds":{"all":90},"dt":1485789600,"sys":{"type":1,"id":5091,"message":0.0103,"country":"GB","sunrise":1485762037,"sunset":1485794875},"id":2643743,"name":"London","cod":200} "

    您只需对代码进行一些更改即可检索

            String jsonString = "your string";
            JSONObject json = new JSONObject(jsonString);
            JSONArray jsonArray = json.getJSONArray("weather");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject weatherObj = jsonArray.getJSONObject(i);
                System.out.println(weatherObj);
            }
    
            String baseValue = json.getString("base");
            Object mainValue = json.get("main");
    
            Object visibilityValue = json.get("visibility");
            Object windValue = json.get("wind");
    

    【讨论】:

      【解决方案3】:

      @shivas 要从源 json 中检索以下 json { "main":"Drizzle","description":"light intensity drizzle","icon":"09d"}],"base":"stations","main":{"temp":280.32,"pressure":1012,"humidity":81,"temp_min":279.15,"temp_max":281.15},"visibility":10000,"wind":{"speed":4.1,"deg":80},请首先检查哪个属性是 JSONArray 和 JSONObject。

      这是检索它的代码。

      main,wind 是 JSONObject,weather 是 JSONArray。

      所以假设 sourcejson 是整个 JSONObject,

      JSONObject main = sourcejson.optJSONObject("main");
      System.out.println(main.toString());
      JSONObject wind = sourcejson.optJSONObject("wind");
      System.out.println(wind.toString());
      JSONArray weather = sourcejson.optJSONArray("weather");
      for (int i = 0; i < weather.length(); i++) {
                  JSONObject weatherObj = weather.getJSONObject(i);
                  System.out.println(weatherObj.toString());
              }
      String base = sourcejson.optString("base");
      int visibility = sourcejson.optInt("visibility");
      

      试试这个,你会得到你的数据。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-18
        • 2017-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多