8

how to properly convert this String to a jsonArray?

{
    "myArray": [
    {   "id": 1,
        "att1": 14.2,
        "att2": false },
    {   "id": 2,
        "att1": 13.2,
        "att2": false },
    {   "id": 3,
        "att1": 13,
        "att2": false }
  ]}

An JSONArray jArray = new JSONArray(STRING_FROM_ABOVE); results in jArray.length = 1 Its my first time to get in touch with json :)

1
  • The string is a JSON object, not an array (note the outermost braces). Commented Aug 13, 2013 at 15:28

4 Answers 4

11

Try this :

JSONObject jObject = new JSONObject(STRING_FROM_ABOVE);
JSONArray jArray = jObject.getJSONArray("myArray");

The "string_from_above" is not a Json Array, it's a Json object, containing one attribute (myArray) which is a Json Array ;)

You can then do :

for (int i = 0; i < jArray.length(); i++) {
        JSONObject jObj = jArray.getJSONObject(i);
        System.out.println(i + " id : " + jObj.getInt("id"));
        System.out.println(i + " att1 : " + jObj.getDouble("att1"));
        System.out.println(i + " att2 : " + jObj.getBoolean("att2"));
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot ... i should really look closer to json and their syntax :)
So you can't say jArray[i]; huh?
can't get you..I have same prb..what is STRING_FROM_ABOVE? and if myArray is STRING_FROM_ABOVE then why should you write "myArray" in JSONArray...
2

While parsing, add values to objects and ArrayLists or however the way you want to use it. Good luck.

JSONObject jo = new JSONObject(STRING_FROM_ABOVE);
JSONArray rootArray= jo.getJSONArray("myArray");
int rootArrayLength=rootArray.length();
for(int i=0;i<rootArrayLength;i++){
   int id=   rootArray.getJSONObject(i).getInt("id");
   // do same for others too and create an object
}
// create object and make a list

Comments

1

Just to throw another method into the mix here, I'd like to recommend taking a look at Gson. Gson is a library that makes serialization to and deserialization from Java objects a snap. For example, with your string, you could do this:

// Declare these somewhere that is on the classpath
public class ArrayItem{
    public int id;
    public double att1;
    public boolean att2;
}

public class Container{
    public List<ArrayItem> myArray;
}

// In your code
Gson gson = new Gson();

Container container = gson.fromJson(json, Container.class);

for(ArrayItem item : container.myArray){
    System.out.println(item.id);  // 1, 2, 3
    System.out.println(item.att1);  // 14.2, 13.2, 13.0
    System.out.println(item.att2);  // false, false, false
}

Similarly, you can go backwards very easily too.

String jsonString = gson.toJson(container);
// jsonString no contains something like this:
// {"myArray":[{"id":1,"att1":14.2,"att2":false},{"id":2,"att1":13.2,"att2":false},{"id":3,"att1":13.0,"att2":false}]}

The primary benefit that using something like Gson provides is that you can now use all of Java's type checking by default, instead of having to manage attribute names and types yourself. It also allows you to do some fancy stuff like replicating type hierarchies that make managing large numbers of json messages a snap. It works great with Android, and the jar itself is tiny and doesn't require any additional dependencies.

Comments

0

Have a look at JQuery, and in particular the Parse function. This will parse a Json Array into an Object directly.

1 Comment

Autocorrect! It's phonetically correct anyway.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.