how to parse JSONArray [duplicate] - android

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

Related

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 save a part of a variable string? [duplicate]

This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 6 years ago.
I get a response but it's different for every user.
I want to extract the UserID & Name. How can I do this?
The response String:
{
"sessionkey":"f4b54dedlfkjhgdlfkjghdfslkjgh1a255242bf71597f4b35ac882f0702",
"user" : {
"userID" : "1",
"name":"Test"
}
}
I want to save 1 = (String userid)
and save Test = (String name)
Thanks in advance
This is just a simple JSON blob, so use a JSON Parser to extract the data.
My lib of choice is org.json.json.
String blob = "{ origin string }";
// create a new json object from the main blob
JSONObject root = new JSONObject(blob);
// extract the session key
String sessionKey = root.getString("sessionkey");
// extract the user object
JSONObject user = root.getJSONObject("user");
// extract the user id
String userId = user.getString("userID");

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

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

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