How to deserialize a like List<MyObject> with GSON [duplicate] - android

This question already has answers here:
How to deserialize a list using GSON or another JSON library in Java?
(4 answers)
Closed 6 years ago.
Hi i serialized a list List<Show> list; to pass it through an intent using
String listSerializedToJson = new Gson().toJson(mShowAdapter.getList());
Now In the other activity I got the json like this:
String listSerializedToJson = getIntent().getExtras().getString("LIST_OF_OBJECTS");
How do I transform it back into a List<Show> list;?

Type listType = new TypeToken<ArrayList<Show>>(){}.getType();
List<Show> yourClassList = new Gson().fromJson(listSerializedToJson , listType);

Related

How to split a string seperated where characters are seperated by , to array in kotlin? [duplicate]

This question already has answers here:
How could I split a String into an array in Kotlin?
(6 answers)
Closed 1 year ago.
val sharedPreferences90 =
getSharedPreferences("sharedPrefs90", Context.MODE_PRIVATE)
val savedString90: String? =
sharedPreferences90.getString("STRING90","null") `
Here my String is company1,company2,company3. I would like to convert it to array. How do I do that?
val result = "company1,company2,company3".split(",").toTypedArray()

how to parse JSONArray [duplicate]

This question already has answers here:
how to parse JSONArray in android
(3 answers)
Closed 2 years ago.
I want to read this JSON lines but because it start with JSONArray i'm a little confused
JSON : https://pastebin.com/zbGXVLJv
I want to retrieve the contents of "img_src"
Kotlin:
val jsonArrayRequest = JsonArrayRequest(url, Response.Listener(){
var data = (JSONArray("photos"))
for (itIndex in 0 until data.length()) {
val item = data.getJSONObject(itIndex)
val imgSrc = item.getString("img_src")
_PhotoList.add(PhotosHubble(imgSrc))
}
Please use Generate POJO from JSON tool of Android Studio and generate different data class then use that data classes to pass in API calling

Gson parses to LinkedTreeMap when serializing ArrayList of Generic Type [duplicate]

This question already has answers here:
Deserialization of generic collections with Gson
(2 answers)
Closed 4 years ago.
I have this JSON string:
[
{
"k":"number",
"v":"055 543 8679\n"
},
{
"k":"hours",
"v":"8:00AM - 9:00PM"
}
]
And I'm trying to parse it using Gson:
val customFields = Gson().fromJson(food.customFields, ArrayList<CustomField>()::class.java)
But somehow when I view it in breakpoint mode, it is parsed as a LinkedTreeMap
try thisval customFields = new Gson().fromJson(food.customFields, CustomField.class);

How to parse this Json with no object name [duplicate]

This question already has answers here:
How do I parse JSON in Android? [duplicate]
(3 answers)
Closed 5 years ago.
{
"quote": "To understand the heart and mind of a person, look not at
what he has already achieved, but at what he aspires to.",
"author": "Kahlil Gibran (1883-1931)"
}
How to parse this with no any object name
There are different methods to parse json object
Simple way
JSONObject myJson = new JSONObject(yourJsonString);
String quote = myJson. getString("quote");
String author = myJson. getString("author");
Another and recomented way is parse json using Gson
For this you can refer below links
How to parse json parsing Using GSON in android
https://www.journaldev.com/2321/gson-example-tutorial-parse-json
How to do it ?
If you meet {} in your code , you can use JSONObject to parse it .
If you meet [] in your code , you can use JSONArray to parse it .
And if you meet [] in your code , you can use for loop to get value in it .
And you should use try catch in your code .
Try this .
try {
JSONObject jsonObject = new JSONObject(response);
String author = jsonObject.getString("author");
String quote = jsonObject.getString("quote");
} catch (JSONException e) {
e.printStackTrace();
}
This is absolutely normal json.
JSONObject json = new JSONObject(your_json);
String quote = json.getString("quote");
String author = json.getString("author");

How do I convert string builder to string array in Android? [duplicate]

This question already has answers here:
StringBuilder to Array of Strings how to convert
(2 answers)
Closed 6 years ago.
I have a string builder in Android which is filled with data from a database, and I want to display that data using a list view. For that I want to covert string builder into an array of strings. Can somebody help me in this conversion, or suggest to me some other technique.
Here you can find Link
tempArray = sb.toString().split("ABCABC"); will split the string and return an array of strings for each line.
Try this :
String sbString = sb.ToString();
String[] ary = "abc".split("TABTAB");

Categories

Resources