0

How would I parse the following string in Android?

{
    "C1": {
        "name": "first name",
        "address": "first address",
        "lat": 36.072111,
        "lng": 34.732112
    },
    "C2": {
        "name": "second name",
        "address": "second address",
        "lat": 32.02132,
        "lng": 34.000002
    },
    "C3": {
        "name": "third name",
        "address": "third address",
        "lat": 37.05435,
        "lng": 34.75703
    }
}

I can't understand. Is it an objects inside of an object structure? How would this be parsed? How do I find how many objects I have?

6
  • Either use the JSONObject class or the Jackson-library. Using Jackson will allow you to map it directly to a POJO. Commented Oct 25, 2013 at 0:14
  • 1
    That's actually a Hash with three objects, C1, C2 and C3 of the type "SomeType". SomeType has 4 fields (name, address, lat, lng). Commented Oct 25, 2013 at 0:14
  • Or Gson (code.google.com/p/google-gson) from Google. Commented Oct 25, 2013 at 0:14
  • I'm using Gson... I just can't understand how would I find the amount of objects I have here if there is more then C1, C2, C3? Commented Oct 25, 2013 at 0:16
  • You have to understand the difference between an Array and a Hash (in JSON). Can there be a C4? Commented Oct 25, 2013 at 0:33

4 Answers 4

3

Well, got it. the solution is to first get the names of the inner-objects:

JONObject json = new JSONObject(jsonString);
JSONArray namesArray = json.names();

which will give you an JSONArray of the existing objects inside. Then run on it's objects to get each one of them:

for (int i = 0 ; i < namesArray.length() ; i ++)
{
    currentObject = json.getJSONObject(namesArray.get(i).toString());
    Log.d("TAG", "currentObject : "+currentObject.toString());          
    addCurrentObjectShopToObjectsListUsingGson(currentObject,objectsList);
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can use JSONObject to extract the contents of the structure.

An example can be shown below:

You can retrieve a JSONArray from your string with

JSONObject json = new JSONObject(jsonString);
JSONArray myArray = json.getJSONArray(ARRAY_NAME_HERE);

After doing so, you can extract the name of a person with

JSONObject person = myArray.getJSONObject(0); // retrieve the first person
String name = person.getString("name"); // get the person's name

Reference:

http://developer.android.com/reference/org/json/JSONObject.html

2 Comments

Yes, but what would be the ARRAY_NAME_HERE in this case?
That can't be done, because the JSON you have is NOT an array.
0

The string you've shown contains an outer object with 3 inner objects. Suppose you want to get C1.name. You would do this:

JSONObject root = new JSONObject(yourString);
JSONObject c1 = root.getJSONObject("C1");
String name = c1.getString("name");

However, I should point out one other thing, which is that the original string you are using is odd because it suggests that what you really want is an array. The code to parse would be different, of course, and involve JSONArray, but I think a better representation would look like this:

  [
   {"name":"first name","address":"...","lat":"...","lng":"..."},
   {"name":"second name"...},
   {"name":"third name"...}
  ]

So in this case, the outermost container is a JSONArray, not an object.

Comments

0

You need a "model" object that looks like this: (provided the hash is static).

public class TheCs extends BaseModel {
    public OneC c1;
    public OneC c2;
    public OneC c3;
}

public class OneC extends BaseModel {
    public String name;
    public String address;
    public float lat, lng;
}

public class BaseModel implements Serializable {
    private static final long serialVersionUID = 1L;//use a random number here
}

Now when parsing with Gson, pass TheCs.class as the type.

If the Hash is not static you could (and Gson will do the right thing as far as I can remember), do something like:

public class TheCs extends BaseModel {
    public List<OneC> someHash;
}

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.