Hello. There are already 3,000,000,000 such questions here. But I don’t understand how to take and learn how to serialize / deserialize Json using GSON.

There is an answer from the server like:

`{"p_result":"ok"}` 

This is an object of type Json, not an array, not anything else.

I need to get this very "ok".

I also have an example of working code, 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 him:

 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 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, even by analogy, cannot do ...

2 answers 2

 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; } } 

PS By the way, the documentation is all very detailed, with a bunch of good examples ...

  • I did it here and did not work: final String resp = execHttpGet (url, nameValuePairList); JSONObject jsonObject = new JSONObject (resp); Result result = new Gson (). FromJson (String.valueOf (jsonObject), Result.class); } - Garf1eld
  • it's not necessary that this is a text, perhaps p_result is a variable enum ... - ProkletyiPirat
  • @ Garf1eld, JSONObject jsonObject = new JSONObject(resp); String.valueOf(jsonObject) JSONObject jsonObject = new JSONObject(resp); String.valueOf(jsonObject) - why is it a two-way conversion? Pass the String once, as shown in the example above. And yes, output resp to the log and show here, perhaps, the response from the server comes with a different JSON structure. - falstaf
  • @falstaf resp = "" That's what's in the log. I check the answer from the server, manually, from the browser. And the answer is presented to you, true. final String resp = execHttpGet (url, nameValuePairList); String json = resp; Log.i ("!!!!!!!", "server_resp:" + resp); Result result = new Gson (). FromJson (json, Result.class); - Garf1eld
  • > resp = "" That's what's in the log. Wait, that is, an empty string comes from the server? Then you should deal with it first. - falstaf

I do not know from the point of view of rationality, but this option is quite working: It is necessary, for example, to deserialize just such a 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"} 

etc.

and you need to pull out only 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); }