从android中的JsonObject读取Json字符串

时间:2016-07-26 11:12:46

标签: android json

我遇到了问题;我无法从jsonObject读取内部字符串。它说JsonArray无法转换为JsonObject。

07-26 13:01:31.910 1798-1901/com.example.phuluso.aafs I/System.out: [{"AccommoAddress":{"AddressID":12,"City":"Johannesburg","InfoUrl":null,"Lattitude":"-26.181321","Longitude":"27.99158","PostalCode":2109,"Street":"22 Ararat Str","Town":"Westdene"},"AccommoDetails":null,"AccommoID":1,"AccommoImages":null,"AccommoName":"West Dunes Properties","AccommoType":"Flat","AccredStatus":"ACCREDITED","AddressId":12,"Capacity":9,"Distance":1,"EndDate":"2017-01-01","NearestCampus":"APK","OwnerId":0,"StartDate":"2016-01-01"}]

这是我的JsonArray。我试图从AccommoAddress读取,但我收到以下错误:

[{"AccommoAddress":{"AddressID":12,"City":"Johannesburg","InfoUrl":null,"Lattitude":"-26.181321","Longitude":"27.99158","PostalCode":2109,"Street":"22 Ararat Str","Town":"Westdene"},"AccommoDetails":null,"AccommoID":1,"AccommoImages":null,"AccommoName":"West Dunes Properties","AccommoType":"Flat","AccredStatus":"ACCREDITED","AddressId":12,"Capacity":9,"Distance":1,"EndDate":"2017-01-01","NearestCampus":"APK","OwnerId":0,"StartDate":"2016-01-01"}]

这是我的代码

    @Override
    protected void onPostExecute(String result) {

        progressDialog.dismiss();
        List<AccommoNearAPK> data = new ArrayList<>();
        progressDialog.dismiss();

        JSONObject jsonResponse = null;

        try
        {
            jsonResponse = new JSONObject(result);
            JSONArray jsonMainNode = jsonResponse.optJSONArray("AccommoAddress");

            /*********** Process each JSON Node ************/

            int lengthJsonArr = jsonMainNode.length();

            for(int i=0; i < lengthJsonArr; i++)
            {
                /****** Get Object for each JSON node.***********/
                JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);

                /******* Fetch node values **********/
                String name       = jsonChildNode.optString("Street");
                String number     = jsonChildNode.optString("City");
                String date_added = jsonChildNode.optString("Longitude");
                String lat = jsonChildNode.optString("Lattitude");

                System.out.print("Street"+ name + "City" +number+ "Long" + date_added+" Lat" + lat);

                Toast.makeText(MapsActivity.this, date_added + name + number + lat, Toast.LENGTH_LONG).show();

            }

        } catch (JSONException e) {
            Toast.makeText(MapsActivity.this, e.toString(), Toast.LENGTH_LONG).show();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

“AccommoAddress”是JSONObject而不是JSONArray。所以不要这样......

JSONArray jsonMainNode = jsonResponse.optJSONArray("AccommoAddress");

试试这个..

/*String Accommo = jsonResponse.getString("AccommoAddress");
JSONObject AccomoAddress = new JSONObject(Accommo);*/
//simplifying the above code
JSONObject Accomoaddress = jsonResponse.optJSONObject("AccomoAddress");
String name = AccomoAddress.getString("Street");
String number = AccomoAddress.getString("City");
String date_added = AccomoAddress.getString("Longitude");
String lat = AccomoAddress.getString("Lattitude");

答案 1 :(得分:-1)

您的回复是JSONArray,而不是JSONObject,类似地,AccommoAddressJSONObject,而不是JSONArray。因此,您需要将顶部附近的行更改为以下内容:

 JSONArray jsonResponse = null;

 try
 {
     jsonResponse = new JSONArray(result);
     JSONObject jsonMainNode = jsonResponse.optJSONObject("AccommoAddress");