This question already has answers here:
Retrofit 2, same name different data type JSON Parsing
(3 answers)
Closed 3 years ago.
I have response like this :
[
{
"id":"all",
"title":"all",
"logo":"http:\/\/185.118.165.197\/services\/all.png",
"title_ar":"\u0627\u0644\u0643\u0644",
"workshop_count":100
},{
"id":1,
"title":"electrical",
"title_ar":"electrical",
"logo":"http:\/\/185.118.165.197\/services\/1.png",
"workshop_count":30
}
]
The problem here is that id is some time String "all" and some time Integer 1,2,.. so what is the best way to handle this multi values of same key?
i am using Retrofit with RxJava
make your id of type Object and then handle the response accordingly
This question already has answers here:
how to parse JSONArray in android
(3 answers)
Closed 7 years ago.
From the server I get a string with exactly the following format:
[{"valueOne", "341", "valueTwo": "1432"}, {"valueOne", "6483", "valueTwo": "3267"}]
I understand that it is two JSONObject into an array, but ..
Howparse this?
My intention is to have all the concatenated string values, like this:
Strings values = (341 + 1432 + 6483 + 3267);
I guess I must first convert the string that I have received from the server to JSONObject, but do not know how to continue.
In this example there are two JSONObjects, but sometimes may contain three or more.
Many times I get values from JSONObjects values, but I have never seen in this case. I searched for information but can not find a solution that is useful to me.
I appreciate the help
greetings!
JsonArray jArray= <your parsed array>;
for(int i=0;i<=jArray.lenght()-1;i++)
{
String valueOne=jArray.getJsonObject(i).getString("ValueOne");
String valueTwo=jArray.getJsonObject(i).getString("ValueTwo");
}
you can do whatever you want with the values.
This question already has answers here:
How do I parse JSON in Android? [duplicate]
(3 answers)
Closed 7 years ago.
In my project, I am getting the following JSON from the HTTP response.
{"base":"USD","date":"2015-04-24","rates":{"AUD":1.2827,"BGN":1.8069,"BRL":2.9733,"CAD":1.2119,"CHF":0.9551,"CNY":6.1948,"CZK":25.364,"DKK":6.8927,"GBP":0.6614,"HKD":7.75,"HRK":7.0284,"HUF":278.53,"IDR":12954.0,"ILS":3.9244,"INR":63.563,"JPY":119.51,"KRW":1078.25,"MXN":15.358,"MYR":3.5741,"NOK":7.8298,"NZD":1.3216,"PHP":44.281,"PLN":3.7076,"RON":4.08,"RUB":51.215,"SEK":8.6674,"SGD":1.3375,"THB":32.55,"TRY":2.7314,"ZAR":12.182,"EUR":0.9239}}
I want to get the "BGN" from the above json. How to get it.
There are multiple steps involved in this
First: Create a jsonObject
JSONObject jObject = new JSONObject(yourJSON_String);
Second: Get your desired Object or Array, in your case
obj = getJSONObject("rates")
Third: Get required String
obj.getString("BGN");
Here is the JSON CLASS REFERENCE
Assuming you are using JSONObject, and that your root document is called "doc",
JSONObject rates = doc.getJSONObject("rates") would get you the rates part:
{"AUD":1.2827,"BGN":1.8069,"BRL":2.9733,"CAD":1.2119,"CHF":0.9551,"CNY":6.1948,"CZK":25.364,"DKK":6.8927,"GBP":0.6614,"HKD":7.75,"HRK":7.0284,"HUF":278.53,"IDR":12954.0,"ILS":3.9244,"INR":63.563,"JPY":119.51,"KRW":1078.25,"MXN":15.358,"MYR":3.5741,"NOK":7.8298,"NZD":1.3216,"PHP":44.281,"PLN":3.7076,"RON":4.08,"RUB":51.215,"SEK":8.6674,"SGD":1.3375,"THB":32.55,"TRY":2.7314,"ZAR":12.182,"EUR":0.9239}
From this object you can then simply get the BGN value using
String bgn = rates.getString("BGN")
JSONObject responseJson = new JSONObject("<your-string">);
JSONObject ratesJson = responseJson.getJSONObject("rates");
double rate = ratesJson.getDouble("BGN");
To see the JSON object clearly, use PrettyJson. Copy paste the JSON string here and it will show you nested objects (if any) for clearly understanding the response.
This question already has answers here:
How do I parse JSON in Android? [duplicate]
(3 answers)
Closed 8 years ago.
How can I parse this json in an android project?
{"code":200,"lang":"en-ru","**text**":["Лучше поздно, чем никогда"]}
I need only a part of "**text**":["Лучше поздно, чем никогда"]
JSONObject jObj = new JSONObject("{"code":200,"lang":"en-ru","text":"Лучше поздно, чем никогда"}");
String text = jObj.optString("text");
Check those links to understand the concept of JSON serialization/de-serialization
http://www.javacodegeeks.com/2013/10/android-json-tutorial-create-and-parse-json-data.html
http://www.vogella.com/tutorials/AndroidJSON/article.html
And here are the 2 most famous library for handling JSON:
GSON
https://sites.google.com/site/gson/gson-user-guide
&
Jackson
http://wiki.fasterxml.com/JacksonInFiveMinutes
This question already has answers here:
Parsing JSON file in android
(4 answers)
Closed 7 months ago.
I need to call this URL: https://login.skype.com/json/validator?new_username=name.surname
I obtain a json file format. I need only to take the element associated to tag "status". For the above link is "status":406, so I need the value 406. What is the most simple way for implementing a parser that get only this element?
Simplest way to do it for this particular situation is
JSONObject obj=new JSONObject(myJSONString);
String status=obj.getString("status");
You'll have to handle the JSONException incase the tag "status" isn't found.
Have you got this response as a string yet? That is your first step but as you are asking for a parser I assume youve done this. If so the rest is easy:
String jsonString = parsedString; //The downloaded JSON String
JSONObject firstObj = new JSONObject(parsedString);
String status = firstObj.getString("status");