Android: parsing json file [duplicate] - android

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

Related

How to convert Json string into Json Array in Android Studio Java

I have a Json String like below
["Monday","Tueday","wednesday","Thuesday","Friday","Saturday"]
What I want to do is send this Json String to a web server using the android studio volley library.
SO I keep getting this error
com.android.volley.ParseError: org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONArray
The string cannot be converted to JsonArray.
So I have 2 questions I need help with.
My first is how to convert string to JsonArray. For example below String.
["Monday","Tueday","wednesday","Thuesday","Friday","Saturday"]
My second question is about passing this Jason Array (once we create) to a web server. I was researching about the Hashmap but I don't know how to send the JsonArray in Hashmap.
I had a go with below code, but it did not work.
#Override
protected Map<String, String> getParams() throws AuthFailureError {
return super.getParams();
}
I don't know how to send JsaonArray in hashmap.
So Overall, I would like to send this above string to web server.
Supose that the name of string is myString.
rString = myString.replace('[','');
rString = rString.replace(']','');
rString = rString.replace('"','');
String split[] = rString.split(',').
Now you have an array called split with every position is a day.
Edit: This is for frist question
Use that
JSONArray yourJsonArray = JSONArray.fromObject(yourJson);
Answer to first question:
I think it's not the exact response from the server, and there's a HTML <br> tag in the response.
because the line Value <br of type java.lang.String cannot be... clearly contains string <br, try replacing the HTML tags and/or decoding/unescaping the HTML tags to plain text, AND then you can convert that plain JSON text to JSONArray.
this snippet might help you out with decoding the response:
String parsedReponse = Html.fromHtml(serverResponse).toString()
if (parsedReponse != null) parsedReponse = parsedReponse.trim();
Answer to second question:
check out OkHttp3 and Retrofit libraries, they're pretty popular and amazing.
those can help you send POST data to your server
hope that helped you :)

Extracting strings out of a String response [duplicate]

This question already has answers here:
How do I parse JSON in Android? [duplicate]
(3 answers)
Closed 5 years ago.
I have following String response. Out of which I want to extract MAC ID & other fields like Version, Type, devIP etc.
Response ={
"Version" : “1.10",
"Type" :"'xyzTYPE",
"MaCID" :"ABCD1F2G3900",
"devIP" : "'192.168.1.100",
"Signal": "-66",
"AreaName" :"'power",
"SubType" :"wifidev",
"'BuiIdTime" : "11:50:47",
"'BuiIdDate": "'Nov 2 2018"
}
Though I have implemented it in this way.
String macID = result.substring(result.indexOf("MaCID")+11,result.indexOf("devIP")-4);
I want to know if there is another sophisticated manner to do the same.
Your response looks like JSON,
so for json response in result;
JSONObject lsubObject= new JSONObject(result);
String MaCID =lsubObject.getString("MaCID");

Parse several JSONOBject into an array in android [duplicate]

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.

Parsing nested JSONObject in Android [duplicate]

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.

How can I parse this json in android project [duplicate]

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

Categories

Resources