JSONObject的错误无法转换为JSONArray

问题描述 投票:0回答:1

我无法将数据分别从API json传递到我的textview我收到“ org.json.JSONObject无法转换为JSONArray”错误

错误:

W/System.err: org.json.JSONException: Value {"message":"accurate","cod":"200","count":1,"list":[{"id":1252948,"name":"Warangal","coord":{"lat":18,"lon":79.5833},"main":{"temp":315.66,"feels_like":314.6,"temp_min":315.66,"temp_max":315.66,"pressure":1002,"humidity":16,"sea_level":1002,"grnd_level":975},"dt":1590404957,"wind":{"speed":2.13,"deg":129},"sys":{"country":"IN"},"rain":null,"snow":null,"clouds":{"all":53},"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04d"}]}]} 
of type org.json.JSONObject cannot be converted to JSONArray
W/System.err:     at org.json.JSON.typeMismatch(JSON.java:112)

这是我的MainActivity代码:

public class MainActivity extends AppCompatActivity {

    TextView temp, sunraise, sunsets, wind, pressure, visibility, humidity;
    EditText search;

    ConstraintLayout constraintLayout;

    ImageButton search_btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        temp = findViewById(R.id.temp);
        sunraise = findViewById(R.id.sunrise);
        sunsets = findViewById(R.id.sunset);
        wind = findViewById(R.id.wind);
        pressure = findViewById(R.id.pressure);
        visibility = findViewById(R.id.visibility);
        humidity = findViewById(R.id.humidity);

        search = findViewById(R.id.your_city);

        constraintLayout = findViewById(R.id.constraintLayout);

        search_btn = findViewById(R.id.search_btn);
        search_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ConnectivityManager ConnectionManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
                assert ConnectionManager != null;
                NetworkInfo networkInfo = ConnectionManager.getActiveNetworkInfo();
                if (networkInfo != null && networkInfo.isConnected()) {
                    new WeatherData().execute();
                } else {
                    Snackbar snackbar = Snackbar.make(constraintLayout, "check your Internet connection", Snackbar.LENGTH_LONG);
                    snackbar.show();

                }
            }
        });
    }

    class WeatherData extends AsyncTask<String, Void, String> {

        String City = search.getText().toString();

        @Override
        protected String doInBackground(String... strings) {

            OkHttpClient client = new OkHttpClient();

            Request request = new Request.Builder()
                    .url("https://community-open-weather-map.p.rapidapi.com/find?type=link%252C%20accurate&units=imperial%252C%20metric&q=warangal")
                    .get()
                    .addHeader("x-rapidapi-host", "community-open-weather-map.p.rapidapi.com")
                    .addHeader("x-rapidapi-key", "a65ed4164bmshecc6a41b1453609p12d370jsn36dc92fffc6d")
                    .build();

            try {

                Response response = client.newCall(request).execute();
                return response.body().string();

            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {

            if (s != null) {

                try {
                    JSONArray jsonObject = new JSONArray(s);
                    if (jsonObject.length() > 0) {
                        JSONArray list = jsonObject.getJSONArray(Integer.parseInt("list"));
                        JSONObject o = list.getJSONObject(Integer.parseInt("0"));
                        JSONObject main = o.getJSONObject("main");
                        String temperature = main.getString("temp");
                        String press = main.getString("pressure");

                            temp.setText(temperature);
                            pressure.setText(press);

                        }

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

    }
}

这是我的JSON:

{
  "message":"accurate",
  "cod":"200","count":1,
  "list":
  [{
    "id":1252948,
  "name":"Warangal",
  "coord":
  {
    "lat":18,
    "lon":79.5833
  },
  "main":
  { 
    "temp":317.1,
    "feels_like":316.2,
    "temp_min":317.1,
    "temp_max":317.1,
    "pressure":1003,
    "humidity":15,
    "sea_level":1003,
    "grnd_level":976

  },
  "dt":1590397763,
  "wind":
  {
    "speed":1.96,
    "deg":117
  },
  "sys":
  {
    "country":"IN"
  },
  "rain":null,
  "snow":null,
  "clouds":{"all":36

  },
  "weather":
  [{
    "id":802,
    "main":"Clouds",
    "description":"scattered clouds",
    "icon":"03d"
  }]

  }]

}

[我正在测试将json输出显示到各自的textview中。代码成功执行后,我将对搜索字段进行功能化以从API中获取数据。

任何人都可以帮助我解决这个错误!

谢谢!

arrays json object
1个回答
0
投票

[在分配if (json instanceof JSONObject)之前先检查其是json数组还是json对象为什么将字符串解析为整数?

© www.soinside.com 2019 - 2024. All rights reserved.