Moshi parse two-dimensional array - android

How to use Moshi+Retrofit to parse the data format of the two-dimensional array type returned by the server
{
"code": 200,
"msg": null,
"data": {
"page": 1,
"size": 10,
"total": 2,
"inner_data": [
[{
"transaction_id": "1626105263092142080",
"creat_time": "1676528680953"
}]
]
}
}

Related

How can I generate a POJO file for this JSON response for auto-deserialization?

I get a JSON response like this from a server:
{
"name_1": [
{
"id": 1,
"type": 1,
"value": "25"
},
{
"id": 2,
"type": 2,
"value": "25"
}
],
"name_2": [
{
"id": 3,
"type": 1,
"value": "25"
},
{
"id": 6,
"type": 4,
"value": "25"
},
{
"id": 7,
"type": 4,
"value": "25"
}
],
"name_3": [
{
"id": 8,
"type": 1,
"value": "25"
}
]
}
How can I generate a POJO file for this in order to use it for auto-deserializing with retrofit?
I tried www.jsonschema2pojo.org, but it wants to create a new class for each JSON object, which is a list (name_1, name_2, name_3), but the count of "name_X" arrays could be different, as well as ther names.
So how can I create a POJO java class for auto-deserializing with GsonConverterFactory in retrofit?
Thanks! :)
You can use Map<String,List<YourObject>> where YourObject contains the id, type and value.
So, response should be :
public class YourObject {
public Map<String, NameDetail> nameDetail;
}
public class NameDetail {
public int id;
public int type;
public String value;
}
And your service with retrofit should return :
Call<YourObject> getStuff();
Your JSON structure is wrong. You should have something like this in order to get list of names:
{
"names" : [
{
"name": "name_1",
"values": [
{
"id": 1,
"type": 1,
"value": "25"
},
{
"id": 2,
"type": 2,
"value": "25"
}
]
},
{
"name": "name_2",
"values": [
{
"id": 3,
"type": 1,
"value": "25"
},
{
"id": 6,
"type": 4,
"value": "25"
},
{
"id": 7,
"type": 4,
"value": "25"
}
]
},
{
"name": "name_3",
"values": [
{
"id": 8,
"type": 1,
"value": "25"
}
]
}
]}
Or if you can't affect server-side then use HashMap<String,List<T>> like Skizo-ozᴉʞS said but it would be the best to change the structure.

RetroFit, Many to many relationships

I have a json response that looks somewhat like this.
{
"id": 1,
"username": "user",
"email": "generic user#gmail.com",
"name": "harshvardhan",
"phone_no": 2147483647,
"created_at": "2016-06-27 12:23:25",
"updated_at": "2016-06-27 12:23:25",
"hash": "",
"groups": [
{
"id": 37,
"title": "aspernatur-et-rem-quos-eius-voluptatem-eveniet-aut",
"descr": "33",
"identifier": "5771545383a2d",
"no_of_members": 0,
"created_at": "2016-06-27 21:59:07",
"pivot": {
"user_id": 1,
"grp_id": 37,
"roles": "admin"
}
},
{
"id": 67,
"title": "quibusdam-voluptas-non-facere-nihil",
"descr": "194",
"identifier": "57715453b8d80",
"no_of_members": 0,
"created_at": "2016-06-27 21:59:07",
"pivot": {
"user_id": 1,
"grp_id": 67,
"roles": "admin"
}
},
{
"id": 161,
"title": "libero-id-ipsa-beatae-aut",
"descr": "59",
"identifier": "5771545469db4",
"no_of_members": 0,
"created_at": "2016-06-27 21:59:08",
"pivot": {
"user_id": 1,
"grp_id": 161,
"roles": "admin"
}
},
{
"id": 198,
"title": "iure-ad-sunt-id-delectus-laboriosam-quo",
"descr": "150",
"identifier": "57715454a9d15",
"no_of_members": 0,
"created_at": "2016-06-27 21:59:08",
"pivot": {
"user_id": 1,
"grp_id": 198,
"roles": "admin"
}
}
]
}
There is a many to many relationship between users and groups.
I am using retrofit with the GsonConverterFactory. On response, how can I parse this information into objects?
I tried making a User class , and a group class, then I put an array of groups as a member of the user class.
But when I access User.getGroups().get(1).getID , it returns 0.
However, User.getGroups().size() does return 4 (the correct number in this case)
How can I achieve this in retrofit?
A practical tool to parse JSON correctly with retrofit can be to generate the classes with http://www.jsonschema2pojo.org
You just have to put the example JSON, the resource type in JSON and put the desired annotation style (GSON, Jackson, ..).
Finally click on the preview button and you can see the results

converting json response (from wordpress REST API version-2 Beta) to json array in Android Studio

Below is the json response from wordpress:
[{
"id": 711,
"date": "2015-09-28T01:00:03",
"date_gmt": "2015-09-27T19:30:03",
"guid": {
"rendered": "http:\/\/www.realcake.in\/?p=711"
},
"modified": "2015-09-28T01:51:52",
"modified_gmt": "2015-09-27T20:21:52",
"slug": "why-only-and-only-realcake",
"type": "post",
"link": "http:\/\/www.realcake.in\/why-only-and-only-realcake\/",
"title": {
"rendered": "Why only and only RealCake"
},
"content": {
"rendered": "<p><img class=\"size-full wp-image-725 aligncenter\" src=\"http:\/\/www.realcake.in\/wp-content\/uploads\/2015\/09\/ONLY-ONLY-REAL-INGREDIENTS-FOR-REALCAKE..jpg\" alt=\"REALCAKE BAKERY IN GURGAON R.\" width=\"940\" height=\"225\" srcset=\"http:\/\/www.realcake.in\/wp-content\/uploads\/2015\/09\/ONLY-ONLY-REAL-INGREDIENTS-FOR-REALCAKE..jpg 940w,
},
"excerpt": {
"rendered": ""
},
"author": 4,
"featured_media": 0,
"comment_status": "open",
"ping_status": "open",
"sticky": false,
"format": "standard",
"categories": [37, 35, 1],
"tags": [],
"_links": {
"self": [{
"href": "http:\/\/www.realcake.in\/wp-json\/wp\/v2\/posts\/711"
}],
"collection": [{
"href": "http:\/\/www.realcake.in\/wp-json\/wp\/v2\/posts"
}],
"about": [{
"href": "http:\/\/www.realcake.in\/wp-json\/wp\/v2\/types\/post"
}],
"author": [{
"embeddable": true,
"href": "http:\/\/www.realcake.in\/wp-json\/wp\/v2\/users\/4"
}]
}
}, {
"id": 420,
"date": "2015-01-01T18:00:07",
"date_gmt": "2015-01-01T12:30:07",
"guid": {
"rendered": "http:\/\/www.realcake.in\/?p=420"
},
"modified": "2015-08-04T23:29:55",
"modified_gmt": "2015-08-04T17:59:55",
"slug": "why-choose-muffins-cakes-realcake",
"type": "post",
"link": "http:\/\/www.realcake.in\/why-choose-muffins-cakes-realcake\/",
"title": {
"rendered": "Why Choose Muffins & Cakes from RealCake Bakery?"
},
"content": {
"rendered": "<div id=\"attachment_423\" style=\"width: 559px\" class=\"wp-caption aligncenter\"><img class=\" wp-image-423\" src=\"http:\/\/www.realcake.in\/wp-content\/uploads\/2015\/03\/Regular-muffins-e1425386051181-300x148.png\"
},
"excerpt": {
"rendered": "<p>. Feel free to write to us or call us if […]<\/p>\n"
},
"author": 4,
"featured_media": 0,
"comment_status": "open",
"ping_status": "open",
"sticky": false,
"format": "standard",
"categories": [35],
"tags": [44, 41, 46, 40, 45, 42, 38, 39, 43],
"_links": {
"self": [{
"href": "http:\/\/www.realcake.in\/wp-json\/wp\/v2\/posts\/420"
}],
"collection": [{
"href": "http:\/\/www.realcake.in\/wp-json\/wp\/v2\/posts"
}],
"about": [{
"href": "http:\/\/www.realcake.in\/wp-json\/wp\/v2\/types\/post"
}],
"author": [{
"embeddable": true,
"href": "http:\/\/www.realcake.in\/wp-json\/wp\/v2\/users\/4"
}]
}
}]
==>> I am using the code below to get JSONArray:
Response response = client.newCall(request).execute();
//here I am getting the json response(mentioned above) from wordpress rest api
String json = response.body().string();
JSONObject object = new JSONObject(json);
//Below code is giving me null array as I dont have "Array name" in my json response.
JSONArray array = object.optJSONArray("Name of Array in json response");
Please help me get JSONArray out of json response.
Looking at your JSON string, it seems that the entire response body is a JSON-Array, so you can simply parse it directly into a instance of JSONArray.
This is how you can do it:
//get your JSON body-string like you do
String json = response.body().string();
//parse it directly into JSONArray
JSONArray array = new JSONArray(json);
//do whatever you want with the array...
I hope this helps you.

How to get Array of array(result values Specially CommentUser[] and BarLike[]) from json string using jackson in Android?

I want to get the value of CommentUser[] and BarLike[] but not able to fetch that array of array from this JSON string .
Please refer this link for View my parsing code :
https://www.dropbox.com/s/rws784060dblbq4/Untitled.png?dl=0
{
"success": "1",
"msg": "Closures found.",
"details": {
},
"result": [{
"name": "Miles Davis",
"birthDate": -1376027600000,
"CommentUser": [{
"like": 0,
"unlike": 1
}],
"BarLike": [{
"like": 0,
"unlike": 1
}]
}]
}

Json Parser returns size=0 Android

How can i look into this Json structure and store each element id name link picutre nb_album nb_fan radio,type into the list. Here is my json file.
{
"data": [
{
"id": "1214294",
"name": "The Pop Rock Boys",
"link": "http://www.deezer.com/artist/1214294",
"picture": "https://api.deezer.com/2.0/artist/1214294/image",
"nb_album": 7,
"nb_fan": 3,
"radio": false,
"type": "artist"
},
{
"id": "813196",
"name": "Ringtone Pop Rock",
"link": "http://www.deezer.com/artist/813196",
"picture": "https://api.deezer.com/2.0/artist/813196/image",
"nb_album": 0,
"nb_fan": 4,
"radio": false,
"type": "artist"
},
{
"id": "4165034",
"name": "Rock of Pop",
"link": "http://www.deezer.com/artist/4165034",
"picture": "https://api.deezer.com/2.0/artist/4165034/image",
"nb_album": 1,
"nb_fan": 0,
"radio": false,
"type": "artist"
},
{
"id": "4022223",
"name": "instrumental / Pop / Rock",
"link": "http://www.deezer.com/artist/4022223",
"picture": "https://api.deezer.com/2.0/artist/4022223/image",
"nb_album": 0,
"nb_fan": 1,
"radio": false,
"type": "artist"
}
],
"total": 4
}
You're parsing the initial string as an array. It isn't an array, its an object that contains a single array named "data". You need to parse it into a JSONObject, then get the array named data from it, then loop like you do.
try {
JSONArray articlesArray = new JSONObject(jString).getJSONArray("data");
JSONObject aObject;
for(int i=0; i<articlesArray.length(); i++){
aObject=articlesArray.getJSONObject(i);
SearchTrack a=new SearchTrack();
a.setId(aObject.getString("id"));
a.setLink(aObject.getString("link"));
a.setName(aObject.getString("name"));
a.setPicture(aObject.getString("picture"));
a.setNbAlbum(aObject.getString("nb_album"));
a.setNbFan(aObject.getString("nb_fan"));
a.setRadio(aObject.getString("radio"));
a.setType(aObject.getString("type"));
articles.add(a);
}
}catch (JSONException e) {
e.printStackTrace();
}

Categories

Resources