简体   繁体   中英

How to parse following JSON using GSON in android?

As keysArray (A,B,C) are dynamic and cores having the same object names ? Please help !!!

{

 "result":"success",

 "keysArray":[
     "A",
     "B",
     "C",
     "D",
     "E"
     ],

 "cores":{

    "A":[{"key":"value"}],
    "B":[{"key":"value"}],
    "C":[{"key":"value"}],
    "D":[{"key":"value"}],
    "E":[{"key":"value"}]
 }
}

I have achieved it in this way.

String responseStr = //JsonResponse here
Type type = new TypeToken<GetMainResponse>() {}.getType();
GetMainResponse getMainResponse = new GsonBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT).create().fromJson(responseStr, type);

    int size = getMainResponse.getKeysArray().size();
    for (int i = 0; i < size; i++) {
        JSONArray jCoreIngredients = new JSONObject(responseStr)
                                            .getJSONArray(getMainResponse.getKeysArray().get(i));
        String innerStr = jCoreIngredients.toString();
        Type type2 = new TypeToken<ArrayList<GetMainResponse.InnerCores>>() {}.getType();
        ArrayList<GetMainResponse.InnerCores> ingredientsInner = new GsonBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT).create().fromJson(innerStr, type2);
    }

//And this is my POJO

public class GetMainResponse{
String result;
ArrayList<String> keysArray;
Cores cores;

public String getResult() { return result; }

public void setResult(String result) { this.result = result; }

public ArrayList<String> getKeysArray() { return keysArray; }

public void setKeysArray(ArrayList<String> keysArray) { this.keysArray = keysArray; }

public Data getCores() { return data; }

public void setCores(Cores cores) { this.cores = cores; }

public class Cores {

// No declaration and not getter setter here.

 public class InnerCores {
    String key;

    public String getKey() { return key; }

    public void setKey(String result) { this.key = key; }

 }
}

Any suggestions :)

For this i will suggest to use Go To JsonSchema2Pojo

To go there and select

  • Source Type : JSON
  • Annotation style: GSON

And click Preview at bottom side You will get the GSON based Model class

Try this out its a sample for getting about us status and description.Based on this code you can make yours. this is the code for your main page.

Gson gson = new Gson();
AboutUsRootObject joc = gson.fromJson(responseOutput.toString(), AboutUsRootObject.class);
resultStatus = joc.status;
resultOutputMsg = joc.message;

then create a new class as AboutUsRootObject.java and write this code:

 public class AboutUsRootObject {


        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public AboutUsData getData() {
            return data;
        }

        public void setData(AboutUsData data) {
            this.data = data;
        }

        public int status ;
        public String message ;
        public AboutUsData data ;
    }

here you have defined getter setter for the data.Here data key has some more data in it multiple, as is its cores key in yours so again create a new class of getter setter similar as above and proceed.

try this:

try {
            JSONObject obj1 = new JSONObject(str);
            Gson gson = new Gson();
            Demo2 demo2 = gson.fromJson(obj1.toString(),Demo2.class);
            data3 = gson.toJson(demo2);
            Log.e("Main Activity",data3.toString());



        }catch (JSONException e) {

            e.printStackTrace();
        }

and Demo2 class :

public class Demo2 {
    private String result;
    private ArrayList<String> keysArray;
    private Core cores;
    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }
    public ArrayList<String> getKeysArray() {
        return keysArray;
    }

    public void setKeysArray(ArrayList<String> keysArray) {
        this.keysArray = keysArray;
    }



    private class Core {
        private ArrayList<ClassD> D;
        private ArrayList<ClassE> E;
        private ArrayList<ClassC> C;
        private ArrayList<ClassB> B;
        private ArrayList<ClassA> A;

        public ArrayList<ClassA> getA() {
            return A;
        }

        public ArrayList<ClassB> getB() {
            return B;
        }

        public ArrayList<ClassC> getC() {
            return C;
        }

        public ArrayList<ClassD> getD() {
            return D;
        }

        public ArrayList<ClassE> getE() {
            return E;
        }

        private class ClassA {
            private String key;

            public String getKey() {
                return key;
            }

            public void setKey(String key) {
                this.key = key;
            }
        }

        private class ClassB {
            private String key;

            public String getKey() {
                return key;
            }

            public void setKey(String key) {
                this.key = key;
            }
        }

        private class ClassC {
            private String key;

            public String getKey() {
                return key;
            }

            public void setKey(String key) {
                this.key = key;
            }
        }

        private class ClassE {
            private String key;

            public String getKey() {
                return key;
            }

            public void setKey(String key) {
                this.key = key;
            }
        }

        private class ClassD {
            private String key;

            public String getKey() {
                return key;
            }

            public void setKey(String key) {
                this.key = key;
            }
        }


    }

    public Core getCores() {
        return cores;
    }

    public void setCores(Core cores) {
        this.cores = cores;
    }







}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM