简体   繁体   中英

Android - parsing JSON strings in Java

I've got a JSON string and I'm just trying to access the different properties of it and store them in Java variables. However, I keep getting an exception with the following code:

    private JSONObject jObj; 
    private String jString; 

//...
jString = result; //this is my JSON string passed from another activity
        try {

            jObj = new JSONObject(jString);
            //int eventID = jObj.getInt("eventID"); 





        } catch (JSONException e) {
            Toast.makeText(searchResultsActivity.this, "Search results failed!", Toast.LENGTH_SHORT).show();
            finish(); 
        } 

Yes I have the required imports. I've displayed jString on its own to confirm that it's valid JSON. I'm kind of lost because this seems to be the most basic thing I need to do. Thanks for any help guys.

EDIT - here is an example JSON string:

[{"eventID":"47","event_name":"test","event_address":"Test","event_duration":"3","event_date":"20110527","event_time":"1347","event_description":"Test","num_attending":"1"}]

This string is received through a PHP script where I do echo json_encode($array), where $array is the associative array creating this JSON response.

The exception I get is:

"org.json.JSONException: Value[//above JSON string//] of type org.json.JSONArray cannot be converted to JSONObject"

Eclipse did not tell you because you were trying to create a JSONObject from a JSONArray:

JSONArray jArr = new JSONArray (jString);
int eventID = jArr.getJSONObject(0).getInt("eventID");

To answer your last comment (why is this?):

From the (original documentation](http://www.json.org/java/index.html):

A JSONObject is an unordered collection of name/value pairs. Its external form is a string wrapped in curly braces with colons between the names and values, and commas between the values and names. The internal form is an object having get() and opt() methods for accessing the values by name, and put() methods for adding or replacing values by name. The values can be any of these types: Boolean, JSONArray, JSONObject, Number, and String, or the JSONObject.NULL object.

A JSONArray is an ordered sequence of values. Its external form is a string wrapped in square brackets with commas between the values. The internal form is an object having get() and opt() methods for accessing the values by index, and put() methods for adding or replacing values. The values can be any of these types: Boolean, JSONArray, JSONObject, Number, and String, or the JSONObject.NULL object.

Since you were parsing a string that started with square brackets instead of curly braces, you need to parse it as a JSONArray. In your case, it is an array of size 1.

You are trying to parse a JSONArray as a JSONObject

JSONArray jarray = new JSONArray(jString);

gl!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM