5

I have a multidimensional array that I want to retrieve using JSON in android. I have the following JSON Structure for this :

  [
      [
        {
          "name": "Banking",
          "data": [
            {
              "vendor_name": "SBI",
              "vendor_id": "1"
            },
            {
              "vendor_name": "ICICI",
              "vendor_id": "2"
            },
            {
              "vendor_name": "BOB",
              "vendor_id": "3"
            }
          ],
          "count": 4
        }
     ]
 ]

I want to get "name",vendor_name,vendor_id in my Strings in android. But I am not getting idea how to do this. Please let me know how to retrieve this.

4
  • tried...after that I post here...!!! :( Commented Sep 15, 2014 at 5:38
  • you want to convert your json array to 2d array for example ya? Commented Sep 15, 2014 at 5:39
  • There are many json parsers in java like gson, jackson etc. Commented Sep 15, 2014 at 5:39
  • stackoverflow.com/questions/17804108/… Commented Sep 15, 2014 at 5:39

2 Answers 2

5
     try {
            jsonObj = new JSONObject(YourString);
        } catch (JSONException e) {
            Log.v("Error in Parser", " " + e);
        }
    try{

     String name=jsonObj.getString("name"); 
            Data = jsonObj.getJSONArray("data");
                for (int i = 0; i < Data.length(); i++) {       
                        JSONObject jsonObj2 = Data.getJSONObject(j);   
                        String vName = jsonObj2.getString("vendor_name");
                        String vId=jsonObj2.getString("vendor_id");     
                }
}catch(Exception e)
{
}
Sign up to request clarification or add additional context in comments.

3 Comments

try above code you will get the desired output!!!
dont just let them copy and paste the code, at least let them know what is happening in your snippet
I wish you explained what the Data class is since there are multiple packages the Android studio asks you to import. While I appreciate your answer, it would have been more helpful if you included some explanation.
2

Try this way,hope this will help you to solve your problem.

try{
   JSONArray jsonArray = new JSONArray("[[{\"name\":\"Banking\",\"data\":[{\"vendor_name\":\"SBI\",\"vendor_id\":\"1\"},{\"vendor_name\":\"ICICI\",\"vendor_id\":\"2\"},{\"vendor_name\":\"BOB\",\"vendor_id\":\"3\"}],\"count\":4}]]");
   HashMap<String,Object> responseMap = new HashMap<String, Object>();
   ArrayList<HashMap<String,String>> dataList =  new ArrayList<HashMap<String, String>>();

   responseMap.put("name",jsonArray.getJSONArray(0).getJSONObject(0).getString("name"));
   for(int i=0;i<jsonArray.getJSONArray(0).getJSONObject(0).getJSONArray("data").length();i++){
       HashMap<String,String> dataRow = new HashMap<String, String>();
       dataRow.put("vendor_name",jsonArray.getJSONArray(0).getJSONObject(0).getJSONArray("data").getJSONObject(i).getString("vendor_name"));
       dataRow.put("vendor_id",jsonArray.getJSONArray(0).getJSONObject(0).getJSONArray("data").getJSONObject(i).getString("vendor_id"));
       dataList.add(dataRow);
   }
   responseMap.put("data",dataList);
   responseMap.put("count",jsonArray.getJSONArray(0).getJSONObject(0).getString("count"));

   System.out.print("name : "+responseMap.get("name").toString());
   ArrayList<HashMap<String,String>> list = (ArrayList<HashMap<String,String>>)responseMap.get("data");
   for (HashMap<String,String> row : list){
        System.out.print("vendor_name : "+row.get("count"));
        System.out.print("vendor_id : "+row.get("count"));
   }
   System.out.print("count : "+responseMap.get("count").toString());
}catch (Throwable e){
    e.printStackTrace();
}

3 Comments

Just friendly suggestion, do not you think there is a need to explain at least very simple sample so novice op can learn ?
@KickButtowski,Dear here i not put any unfamiliar or not understable code so any one know json easily understood whatever i have done here and last thanks for you suggestion
And what About those of use who don't ...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.