0

I have a api like this

-{
    - meta {
        item1: value;
        item2: value;
        item3: value;
      },
    - object [
        {
            - category {
                id: 1;
                ...
              }
            - File {
                ...
              }
        },
        {
            - category {
                id:2;
                ...
              }
            - File {
                ...
              }
        }
      ]

I dont have any problem to parse JSONArray of "obejct" and it's JSONObjects... I do like this:

private static final String TAG_NOD = "object";
private static final String TAG_CAT = "category";
private static final String TAG_CAT_ID = "id";
private static final String TAG_CAT = "File";
JSONArray Items = null;

JSONObject jsonObj = new JSONObject(jsonstr);    // jsonstr is loaded url by httpcal

Items = jsonObj.getJSONArray(TAG_NOD);
for ( int i=0; i<Items.length(); i++){
    JSONObject childs = Items.getJSONObject(i);

    JSONObject category= child.getJSONOBject(TAG_CAT);
    // getting category items in string like String ID = category.getString(TAG_CAT_ID);

    JSONObject file = Item.getJSONObject(TAG_FILE)
    // getting file items in string like file.getString(String name)

    HashMap<String, String> items = new HashMap<String, String>();
    items.put(TAG_CAT_ID, ID);
    .
    .
    .


    ItemLis.add(items);    //Itemlist is a ArrayList<HashMap<String, String>>()

}

but my problem is to parsing jsonobject of "meta" and add its data to my ListItem Arraylist any solution appreciated :D

2
  • 1
    Use JSONObject json_meta = jsonObj.getJSONObject("meta"); to get meta JSONObject from JSONObject Commented Apr 6, 2014 at 8:45
  • i want to have "meta" object child like item1. item2,..... Commented Apr 6, 2014 at 8:47

2 Answers 2

2

Like this:

JSONObject meta = jsonObj.getJSONObject("meta");
Iterator<String> iterator = meta.keys();
HashMap<String,String> metaMap = new HashMap<String,String>();
while (it.hasNext()) {
    String key = iterator.next();
    String value = meta.getString(key);
    metaMap.put(key,value);
}
ItemLis.add(items);

Also, if I may, here are a couple of quick suggestions on some things I've noticed that could improve your coding style:

  1. Per Java naming conventions, variables and member names start with a lower case letter, so it should be itemLis instead of ItemLis. You already do this on most of your names except for ItemLis and Items. While this is not a must, following convention will make your code more readable, not only to you, but also when you seek help here on SO.

  2. Unless you specifically require that the items in ItemLis be HashMaps (that is, your algorithm will not work with any other Map implementation), it's usually considered good practice to hide that detail from the implementation. I.e. instead of HashMap<...> items = new HashMap, go like Map<...> items = new HashMap, and change the type of ItemLis from <ArrayList<HashMap<...>> to List<Map<...>>. That way, if you ever decide to change the implementation backing the list or map, your code changes will be confined to only one place, instead of breaking much of your code. As a rule of thumb: Try to declare types with the most abstract type you require.

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

3 Comments

Thanks Chris. now i have key and value but i cant put them in my Arraylist... how can i do this?!
I can't tell you that, because frankly, I'm a little confused about why completely different data should end up in the same list. In what format are you expecting it in there? Should each meta item be its own Map? Or should they all be in the same map? Maybe you can give an example of what you're expecting your array list to look like at the end of the algorithm.
i want to have them in same Map
2

You can use the GSON lib. Very simple and fast implemented. Best practise at this case.

Just create an object which looks like your json string.

Then call:

Gson gson = new Gson();
YourObject obj = gson.fromJson(jsonString, YourObject.class);

Then you have the data in an object and can use it with getter and setter. Maybe you have to annotate the variables in the object. For that take a look here: https://code.google.com/p/google-gson/

1 Comment

This is a lib from google it self. I promise it will be never easier. I tried several ways but the gson lib is the best and easiest way.

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.