Parse several JSONOBject into an array in android [duplicate] - android

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.

Related

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

How to parse nested JSON object in android [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to parse the verse_nr and verse from the bellow JSON data.can anyone help me..
{"book":[{"book_ref":"Ps","book_name":"Psalms","book_nr":"19","chapter_nr":"16","chapter":{"8":{"verse_nr":"8","verse":"I have set the LORD always before me: because he is at my right hand, I shall not be moved."}}}],"direction":"LTR","type":"verse","version":"kjv"}
You'll need to use a JSON Parser library. Here's an example with org.json. parser.
JSONObject root = new JSONObject(inputJSON);
JSONObject chapter = root.getJSONArray("book")
.getJSONObject(0).getJSONObject("chapter");
JSONObject verse = chapter.getJSONObject(JSONObject.getNames(chapter)[0]);
Basically, you just chain the JSON getters till you reach the verse object. Once there, you can access the values with getString() as
System.out.println(verse.getString("verse_nr"));
System.out.println(verse.getString("verse"));
Output :
8
I have set the LORD always before me: because he is at my right hand, I shall not be moved.
You must iterate over it using JSONObject and JSONArray.
You can generate classes using: http://www.jsonschema2pojo.org/ Just make sure to check 'Json' as opposed to 'Json Schema' since this is the raw json. Then you can use gson to automatically turn this into the those generated classes. A guide on doing so:
http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html
If this is coming from a server, I'd recommend using the wonderful Retrofit library which would allow you to do all the above with just a few lines of code; a tutorial of doing this can be found here: http://inaka.net/blog/2014/10/10/android-retrofit-rest-client/
Anyways if want to ignore making your life easier with the above solutions, to manually do this you'll do something like the following:
String jsonLiteral = /* your json remember to escape the double quotes */
JSONObject root = new JSONObject(jsonLiteral);
// Get Strings from a JSONObject as
String direction = root.getString("direction");
// You have a single element array, so you need to get that
JSONObject singleElement = root.getJSONArray("book").getJSONObject(0);
// Then you can Strings from it like direction above
String bookRef = singleElement.getString("book_ref");
You can also get a pretty output to inspect the json structure by using:
http://jsonformatter.curiousconcept.com/

Sending Integer to Json Call

Actually "Strictly speaking, json is untyped, so you can't send it as an integer, it has to be a string." is said in that question 2 YEARS AGO. So i wonder if there are any changes in JSON to get integer from a JSON Object. I try to call integer by this way;
static int versionCode=0;
jsonObject = new JSONObject(result);
JSONObject queryJSONObject = jsonObject.getJSONObject("query");
versionCode =queryJSONObject.getInt("versionCode");
And the JSON is like this;
{"query":{"versionCode":1,....}}
Can anything be done?
Thanks.
follow this Tutorial you can use getString() function to get value and latter you can convert it like this int foo = Integer.parseInt("1234"); in integer according to your need
JSON has a number element, see JSON.org
JSON is a sequence of characters. But I disagree that a number is a string in this context. IMHO for me a string is wrapped in double quotes (which a number isn't).
Your code should work: json.getInt("versionCode") Source from Android Developer

How to extract the string variable in android? [duplicate]

This question already has an answer here:
How to extract this string variable in android?
(1 answer)
Closed 9 years ago.
String test=["1","Low-level programming language",true]
Here i want to extract this string value and as i need to get only second value like "Low-level programming language".How to get this value using string functions in android?
Per your comment, I'm assuming that you have a single string that contains the entire text (including the brackets). In general, splitting comma-separated values is a fairly tricky process. For your specific string, though, it's kind of easy:
String test = "[\"1\",\"Low-level programming language\",true]";
String[] pieces = test.split(",");
String middle = pieces[1];
// now strip out the quotes:
middle = middle.substring(1, middle.length() - 1);
In general, you might want to look at using a general CSV parser like Apache Commons CSV or openCSV.
Alternatively, if this is JSON data (which looks more likely than CSV), take a look at using one of the Java JSON libraries listed here (scroll down the page to see the list).

Android: parsing json file [duplicate]

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

Categories

Resources