GSON Android, well, I don't understand it at all


Hello. There are already 3,000,000,000 such questions. But I do not understand at all how to take and learn how to serialize / deserialize Json, using GSON.

There is a response from the server of the form:

    `{"p_result":"ok"}`

This is a Json object, not an array, not anything else.

I need to get this very "ok".

I also have an example of working code for getting values from an array:

    User.java

public class User {
private String p_id;
private String p_name;
private String p_last_name;

public String getId() {
    return p_id;
}

public void setId(String id) {
    this.p_id = id;
}

public String getName() {
    return p_name;
}

public void setName(String name) {
    this.p_name = name;
}

public String getLastName() {
    return p_last_name;
}

public void setLastName(String lastName) {
    this.p_last_name = lastName;
}
}

To it:

public class JsonWorker {

public static List<User> jsonToUserList(String json)
{
    Gson gson = new Gson();
    Type listType = new TypeToken<List<User>>() {}.getType();
    return gson.fromJson(json,listType);
}

}

And the method:

public static List<User> getUserFromServer() throws IOException, JSONException {

    final List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
    nameValuePairList.add(new BasicNameValuePair("query", "spr_employee_get"));
    nameValuePairList.add(new BasicNameValuePair("p_guid", user_guid));
    final String resp = execHttpGet(url, nameValuePairList);
    JSONObject jsonObject = new JSONObject(resp);
    if (jsonObject.has("p_result")) {
        if (jsonObject.getString("p_result").equalsIgnoreCase("ok"))
            return JsonWorker.jsonToUserList(jsonObject.getJSONArray("p_item").toString());

    }
    return new ArrayList<User>();
}

And I can't even do it by analogy...

Author: Garf1eld, 2014-06-03

2 answers

String json = "{\"p_result\":\"ok\"}";
Result result = new Gson().fromJson(json, Result.class);

public class Result {
    @SerializedName("p_result")
    private String status;

    public String getStatus() {
        return status;
    }
}

P.S. By the way, in the documentation everything is very detailed, with a bunch of visual examples...

 10
Author: falstaf, 2014-06-03 11:47:45

I don't know from the point of view of rationality, but this option is quite working: For example, you need to deserialize this line:

{"photos":{"page":1,"pages":10,"perpage":100,"total":1000,"photo":[{"id":"31692962564","owner":"38589282@N03","secret":"69a4da0d87","server":"596","farm":1,"title":"Wall","ispublic":1,"isfriend":0,"isfamily":0,"url_s":"https:\/\/farm1.staticflickr.com\/596\/31692962564_69a4da0d87_m.jpg","height_s":"180","width_s":"240"},{"id":"31692962804","owner":"136025614@N02","secret":"cc66d3581f","server":"731","farm":1,"title":"Completely Hiding Blogger Widgets from Certain Pages","ispublic":1,"isfriend":0,"isfamily":0,"url_s":"https:\/\/farm1.staticflickr.com\/731\/31692962804_cc66d3581f_m.jpg","height_s":"167","width_s":"240"}

And so on.

And you only need to tear out the fields id, title, url_s.

public class GalleryItem {

  @SerializedName("title")
  private String mCaption;

  @SerializedName("id")
  private String mId;

  @SerializedName("url_s")
  private String mUrl;
}

public void deser() {
  List<GalleryItem> items = new ArrayList<>();
  String jsonString = ""{"photos":{"page":1,"pages":10,"perpage" и тд...";
  JSONObject jsonBody = new JSONObject(jsonString);
  JSONObject photosJsonObject = jsonBody.getJSONObject("photos");
  JSONArray photoJsonArray = photosJsonObject.getJSONArray("photo");
  Gson gson = new Gson();
  Type collectionType = new TypeToken<List<GalleryItem>>(){}.getType();
  items = gson.fromJson(photoJsonArray.toString(), collectionType);
}
 0
Author: Алексей Боксеров, 2017-01-26 16:54:20