Hello. There are already 3,000,000,000 such questions here.
But I do not see how to take and learn serialization / deserialization JSON, using GSON.
There is an answer from the view server:
`{" p_result ":" ok "}`
This is a JSON type object, not an array, not anything else.
I need to get this very “OK”.
Also, I have an example of a working code, receiving from an array of values:
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 Setname (String Lastname) {
this.p_last_name = lastname;
}
}
to it:
Public Class JSONWORKER {
Public Static List & LT; User & gt; JSONTOUSERLIST (String Json)
{
GSON GSON = NEW GSON ();
Type Listtype = New TypeToken & lt; List & lt; User & gt; & gt; () {} .gettype ();
Return Gson.Fromjson (JSON, ListType);
}
}
and method:
public static list & lt; user & gt; getUserFromServer () Throws IoException, JSonexception {
Final List & LT; NameValuePair & GT; namevaluepairlist = new arraylist & lt; namevaluepair & gt; ();
namevaluepairlist.add ("Query", "SPR_Employee_get"));
namevaluepairlist.add (new basicnamevaluepair ("p_guid", user_guid));
Final String Resp = Exchttpget (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 & LT; User & gt; ();
}
And I can’t even do …
Answer 1, Authority 100%
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 documentation everything is described in very detailed, with a bunch of visual examples …
Answer 2
I do not know from the point of view of rationality, but this option is quite worker:
It is necessary, for example, 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 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 & lt; GalleryItem & gt; Items = New ArrayList & lt; & gt; ();
String JSONSTRING = "" {"PHOTOS": {"Page": 1, "Pages": 10, "PERPAGE" and TD ... ";
JSONOBJECT JSONBODY = NEW JSONOBJECT (JSONSTRING);
JSONObject photosJsonObject = jsonBody.getJSONObject ("photos");
JSONArray photoJsonArray = photosJsonObject.getJSONArray ("photo");
Gson gson = new Gson ();
Type collectionType = new TypeToken & lt; List & lt; GalleryItem & gt; & gt; () {}. GetType ();
items = gson.fromJson (photoJsonArray.toString (), collectionType);
}