FaJa9211
FaJa9211

Reputation: 61

How to get Json data

I have a json array like this

{
   "success":"true",
   "crops": {
           "Brinjal":[---varieties of brinjal---],
           "apple":[---varieties of apple---],
              .................
              .................
        }
}

So i have got the complete crops using.....

JSONObject jsonObject;
jsonObject.getString("crops")

but i actually need to create two arrays like

String[] crops = {"Brinjal","apple"};
String[] varities = {"---Brinjal Varities---","---apple varities---"};

how can I generate these two arrays if i generate "crops" then i can generate other array.... so how can i generate the "crops"...

Upvotes: 0

Views: 97

Answers (3)

Ray Q
Ray Q

Reputation: 11

public static void  main(String[]args){
    String joson = "{\n" +
     "   \"success\":\"true\",\n" +
     "   \"crops\": {\n" +
     "           \"Brinjal\":[---varieties of brinjal---],\n" +
     "           \"apple\":[---varieties of apple---],\n" +
     "              .................\n" +
     "              .................\n" +
     "        }\n" +
     "}";
    List<Object> apple = new ArrayList<Object>();
    List<Object> Brinjal = new ArrayList<Object>();
    apple.add("---varieties of apple---");
    Brinjal.add("---varieties of brinjal---");
    Map<String,Object> mapF = new HashMap<String,Object>();
    Map<String,Object> mapS = new HashMap<String,Object>();
    mapS.put("Brinjal", Brinjal);
    mapS.put("apple", apple);
    mapF.put("success", "true");
    mapF.put("crops",mapS );

JSONObject obj = new JSONObject().fromObject(mapF);
System.out.println(obj);

JSONObject objF = obj.getJSONObject("crops");
String[] ListF = {};
String str = objF.keySet().toString();
String strF = str.substring(1, str.length()-1);
ListF = strF.split(", ");
List<Object> lsitF = new ArrayList<Object>();
for(int i = 0;i<ListF.length;i++){
    lsitF.add(objF.get(ListF[i]));[enter image description here][1]
}
System.out.println(str);
System.out.println(lsitF.toString());

}

Hope it helps you.

Upvotes: 1

FrancescoAzzola
FrancescoAzzola

Reputation: 2654

You should do:

JSONObject obj = new JSONObject(jsonStringResp);
JSONObject cropsObj = obj.getJSONObject("crops");
JSONArray  arr1 = subObject.getJSONArray("Brinjal");

Then you iterate over the JSON array and create the String array. Hope it helps you.

Upvotes: 4

Quang Doan
Quang Doan

Reputation: 662

Try this:

 String joson = "{\n" +
            "   \"success\":\"true\",\n" +
            "   \"crops\": {\n" +
            "           \"Brinjal\":[---varieties of brinjal---],\n" +
            "           \"apple\":[---varieties of apple---],\n" +
            "              .................\n" +
            "              .................\n" +
            "        }\n" +
            "}";
    List<String> whereCrops = new ArrayList<String>();
    List<String> whereVarities = new ArrayList<String>();
    String[] crops;
    String[] varities;
    ArrayList<String> contentKey = new ArrayList<String>();
    try {
        JSONObject myObject = new JSONObject(joson);

        JSONObject jsonCrops = myObject.getJSONObject("crops");
        Iterator<String> iter = jsonCrops.keys();

        while (iter.hasNext()) {
            String key = iter.next();
            whereCrops.add(key);

            Log.e("inningskey", key);
            try {


                JSONArray array = jsonCrops.getJSONArray(key);
                whereVarities.add(array.toString());


            } catch (JSONException e) {
                // Something went wrong!
            }
        }
         // here ! you need convert whereCrops to crops
    } catch (JSONException e) {
        e.printStackTrace();
    }


}

Upvotes: 0

Related Questions