0

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"

5
  • 3
    What is the json exception, and what is the json string pls? What you're doing should work. Commented May 27, 2011 at 19:04
  • I edited my post with the string and the error. Uh oh, do I need to import some JSONArray thing? Eclipse didn't tell me =( Commented May 27, 2011 at 19:16
  • I agree with Taylor. If you're catching a JSONException, then jString is not a valid JSON string. Commented May 27, 2011 at 19:17
  • Aww crap, ok I figured out how to make it work. Basically I just change type to JSONArray (and import JSONArray) and everything works out fine. Out of curiosity, why is this? Commented May 27, 2011 at 19:19
  • because you're pulling an array, not an object. Commented Jun 5, 2011 at 17:04

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

You are trying to parse a JSONArray as a JSONObject

JSONArray jarray = new JSONArray(jString);

gl!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.