how do I save a part of a variable string? [duplicate] - android

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

Related

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

How to read a JSON value? [duplicate]

This question already has answers here:
Parsing JSON Object in Java [duplicate]
(5 answers)
Closed 5 years ago.
this is my json response
{
"status": true,
"code": 200,
"message": "Success",
"data": {
"id": "14"
}
}
how to read this values
You can use the help of Google's GSON library built for exact your pupose.
YourPojoClass model = new Gson().fromJson(jsonResponse, YourPojoClass.class);
Convert your json to PojoModel by using jsonschema2pojo.org
You can do it like this:
Create a JSONObject:
JSONObject jObject = new JSONObject(result);
To get a specific boolean
String aJsonString = jObject.getBoolean("status");
To get a specific String
String aJsonString = jObject.getString("message");
To get a specific Integer
int aJsonInteger = jObject.getInt("code");

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 to Split response from Json in android?

I am developing an app in which I got response like "100.0" from server which I have to split to "100" and save in variable.How can I do that kindly tell me .
You can use this code to parse your JSON response. Store your wallet balance in a String, them trim the value as per your request
JSONObject object = new JSONObject(JsonResponseText);
String walletBal = object.getString("wallet balance");
String trimWalletBal = walletBal.subString(0,walletBal.indexOf('.'));
You can do it this way:
Integer value = jsonObject.getDouble("parameter").intValue();
Assuming that your problem is just to split the "Integer" and "Fraction" part of a "string" value you are getting from the server -
int decimalIdx = stringVal.indexOf('.');
if (decIdx != -1) {
return stringVal.substring(0, decIdx);
}
return stringVal;

In which form the data of facebook graph api me/home should be stored?

I'm getting some user data with API /me/home. I want to ask that in which form shall I store the result- in JSONArray or JSONObject?
Please tell me the method to store data. I am using following procedure to get user data i am storing it in string now.
if (fb.isSessionValid()) {
button.setImageResource(R.drawable.logout_button);
pic.setVisibility(ImageView.VISIBLE);
JSONObject obj=null;
JSONArray feed = null;
URL img_url = null;
try {
String JSONUser = fb.request("me");
obj = Util.parseJson(JSONUser);
String id = obj.optString("id");
String name = obj.optString("name");
String newsfeed=fb.request("me/home");
Please help me.
The result of /me/home will be of form-
{
"data":
[
.. .. ..
]
}
So, take the result in a JSONObject, then for the key data save the result to a JSONArray and then use the data.
You can always check the results of the Graph API in Graph API Explorer.

Categories

Resources