0

can any one help me to parse this json using gson library. I could not create the correct POJO class for this Json

{"status":"ok","result":[{"name":"Al Mansoori Villa in Al Khawaneej","reference":"UB5647","link":"\/index.php?r=apiv1\/projects\/view&id=21570"},{"name":"Mr. Mohammad Dhanhani Villa in Dibba","reference":"UB6046","link":"\/index.php?r=apiv1\/projects\/view&id=22970"},{"name":"Villa in Al Barsha","reference":"UB6664","link":"\/index.php?r=apiv1\/projects\/view&id=25877"},{"name":"Bin Omeir Hospital in Abu Dhabi","reference":"UB6054","link":"\/index.php?r=apiv1\/projects\/view&id=23291"}]}

Thanks in advance

WEFX
  • 8,298
  • 8
  • 66
  • 102
Ansar
  • 364
  • 3
  • 16

4 Answers4

3

check this link for Parsing JSON Array using GSON This might help you...

Community
  • 1
  • 1
i.n.e.f
  • 1,773
  • 13
  • 22
1

I was able to parse this using POJO. You have to make sure that under Annotation style you select GSON and under Source type: you select JSON. Then in your code convert to

Single Object

/**
 * This will convert json string to User object.
 * @param jsonUserString
 * @return <b>User object/b>
 */
public User convertFromJsonStringToUserObject(String jsonUserString){
    Gson g = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
    Type listType = new TypeToken<User>() {}.getType();
    return g.fromJson(jsonUserString, listType);
}//END 

ArrayList of objects.

/**
 * Convert string to {@link ArrayList} of {@link Address}
 * @param jsonAddress <b>String</b> json string
 * @return <b>{@link ArrayList} of {@link Address}</b>
 */
public ArrayList<Address> convertJsonStringToAddressObjectArray(String jsonAddress){
    ArrayList<Address> addressList = new ArrayList<Address>();
    Type listType = new TypeToken<List<Address>>() {}.getType();
    Gson g = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
    addressList=g.fromJson(jsonAddress, listType);
    return addressList;
}//END
dsum27
  • 505
  • 3
  • 12
1

You will need to have a class as per your response:

In your case something like shown below:

public class MyResponse{
    public String status = "";
    public List<Result> result;
    public MyResponse{
        result = new ArrayList<MyResponse.Result>();
    }

    //getter setter for status and result

    public class Result{
        public String name="";
        public String reference="";
        public String link="";
        public Result(){

        }
        //getter setter for name, reference, link
    }

}

And then just parse it as:

MyResponse response = new Gson().fromJson(responseString,
                    MyResponse.class);

Hope this helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
1

Response seems a straightforward one with the String of status and an arrayList of "Result"(s)

To do it directly using Gson, you can set your model class in the same way with an independent class for Result array objects.

Should work with:

class JsonResponseModel{

String status;

@SerializedName("result") //optional to use, you can name the variable as "result"
ArrayList<ResultInResponse> resultArrayList;

//getters setters
//toString for the class, works well to verify data populated
}

Now the result class

class ResultInResponse{

String name;

String reference;

String link;

//getters setters
//toString for the class
}

Implementation:

Gson gson = new Gson();

jsonResponseModel = gson.fromJson(yourStringContent,
                            JsonResponseModel.class);

Easy to map it that way.

Pararth
  • 8,114
  • 4
  • 34
  • 51