Suppose I have the following JSON -
{
"list": [
{
"name": "John",
"age": 20
},
{
"name": "Peter"
"age": 18
},
{
"name": "Jane",
"age": 21
}
]
}
Is there a way in moshi in which we can skip the list item where the name is "Peter"?
My issue is that I need to show a list of items only if they match with certain values (eg. "imageType" = "jpeg") provided in json and skip everything else. I am wondering if one can do the skipping at the json parsing level rather than somewhere in the business logic where I would have to run a for loop and go through every element again.
Related
I have a very large json file within my android app with the following struture:
[
{
"id": 123,
"data": {
...
}
},
{
"id": 456,
"data": {
...
}
},
...
]
Depending on the item selected in the UI, I need to deserialize only the item with the corresponding id, but right now I'm deserializing the whole file and then do the filter, which takes time and memory to just extract the id I need.
Is there a way to deserialize only the section I need?
I'm using kotlinx.serialization.
I have this json response from the server:
[
{
"id": 12767236,
"rowNumber": "01",
"seatNumber": 1,
"status": "taken"
},
{
"id": 12767237,
"rowNumber": "01",
"seatNumber": 2,
"status": "available"
}
...
...
...
]
Basically it's a list of seats with a few properties, that's all the data I have. So, question is how do I show the seats and let user pick one like below? What component should I use?
You can use GridLayoutManager but you have to have a dummy/empty item to represent those rows, but you will need logic to assign those specific items to the list
I am working on an ionic app which will be used on android and iOS platforms.and I need to post an object to the server which contain some attribute that reference others for example:
{
"room": [
{
"#id": 2,
"date": "2019-10-10",
"number": "750"
}
],
"bed": [
[
{
"class": "A",
"room": 2
}
]
]
}
but when posting it the order of the JSON object changes to :
{
"bed": [
[
{
"class": "A",
"room": 2
}
]
],
"room": [
{
"#id": 2,
"date": "2019-10-10",
"number": "750"
}
]
}
what should I do to keep the order of the object, considering that it works fine in android?
Referring to JSON's standards, an object is simply unordered which means there is no problem with your case. However, if in your case the order is important, you may use arrays. Arrays make sure the order is preserved.
I am trying to fetch JSON Object which has 2 list object with the same name, but has a different value. The object list named "items", the first "items" has "products_title,product_image,link", and the second "items" has "article_title, article_image,link". How to write PODO ?
I've been trying to writes PODO, but even i try to change the model, it still does not work. I try the other REST API, for the example "https://jsonplaceholder.typicode.com/pothos" its working fine. But if i try used my JSON its getting error, i wonder how to write PODO ?
this is the JSON i am using :
{
"data": [
{
"section": "electronics",
"items": [
{
"product_name": "Cellphone",
"product_image": "cellphoneImage.png",
"link": "https://cellphone.html"
},
]
},
{
"section": "facts",
"section_title": "Title section",
"items": [
{
"article_title": "Facts",
"article_image": "https://www.facts.png",
"link": "https://www.facts.html"
},
]
}
]
}
The JSON result for getUsers I get from the server looks like this:
{
"result": [
{
"meta": {
"rows": "3"
}
},
{
"items": [
{
"id": "1",
"name": "Steve",
"age": "30"
},
{
"id": "2",
"name": "Mary",
"age": "29"
},
{
"id": "3",
"name": "Bill",
"age": "58"
}
]
}
]
}
How can I deserialize it by GSON in my android app (I'm using retrofit)?
I can't imagine any wrapper classes because of the different object types in result.
Any help would be appreciated!
For good example
Converting JSON to Java
Other way, you can convert your json to a java object
Please use org.json library http://www.json.org/java/index.html
Then, for example
json = new JSONObject(YOUR_JSON).getJSONObject("result");
JSONArray items = data.getJSONArray("items");
String name = items.getJSONObject(0).getString("name");
You can write a TypeAdapter for a type that will be (de)serialized to(from) array. You can even make it generic, so it will work with type like Pair<A, B>. Here is an example for non-generic type: https://github.com/cakoose/json-tuple-databinding-examples/blob/master/java/src/GsonEntryCustomizer.java — it (de)serializes Entry to(from array).
Disclaimer — I have not written nor tested that code, but it seems legit.
If you only encounter such problem once (like in your example), you may not bother making it generic, just write TypeAdapter for your specific pair of 2 different classes. The reading code is quite straightforward:
in.beginArray();
SomeClass1 info1 = gson.getAdapter(SomeClass1.class).read(in);
SomeClass2 info2 = gson.getAdapter(SomeClass2.class).read(in);
in.endArray();
return new SomeContainerClass(info1, info2);
(see https://github.com/cakoose/json-tuple-databinding-examples/blob/master/java/src/GsonEntryCustomizer.java#L52)