How to get Json String without set data in getString("name")? - android

Im doing a dynamic form to Android and I need just read a Json file and build a form and it should work in anyway. How can I do it? In "name" getString("name") what I can put there to get a name without write "name" inside de method?

Is the question that you want to find the keys of a JSONArray? Iterate through the array, grab the key of each object. Then, grab the value.
See second answer: Retrieving Keys from JSON Array key-value pair dynamically - Javascript

I think the following should help:
http://developer.android.com/reference/org/json/JSONObject.html#keys()
You can use this Iterator to iterate through all the keys and then use the key to extract the desired values.
Additionally, if dealing with a JSONArray you could use the following too:
http://developer.android.com/reference/org/json/JSONObject.html#names()

Related

Hello, I am receiving a json string, I need to separate data by class properties

I am receiving a json string, I need to separate data by class properties. The problem is that I can only know part of the key. For example, the key is 12345#666.777, I only know #666.777. Is it possible to somehow use regular expressions or look for values by part of the key?
I would iterate over all the keys in the JSON object as shown here: https://discuss.kotlinlang.org/t/iterating-over-json-properties/1940/2
and check for every key if it contains the partial key I have as a substring using this standard Kotlin method:
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/contains.html
You also need to treat cases where the same substring is present in 2 different keys (if you think that is possible).

Creating json object without keys from array in android

Is there any way to create json object with only values and no keys, from array list in android?
I have array list like this
[12,name,description]
But i need to convert this to JSON Object like this
{"12","name","description"}
How to do this? Can anyone help?
No, you cannot create JSONObject without its key.
A modifiable set of name/value mappings. Names are unique, non-null
strings.
Source
Definition itself says names are unique, non-nullso you need to use key-value pair only, however it it possible to create JSONArray like this, but in that case also that JSONArray have some key.
"someKey":["a","b","c"]
So in this case also JSONArray needs to define under some key.

Get JSON Object Keys in a loop with default order android

I want to parse JSONObject and get its keys in the order they do represent to me when i receive. I don't want use JSONObject.keys() as this function giving undefined order of json keys and fully unordered,and this is a huge problem right now because I'm stuck in this position and i have to get json object keys.So is there any way to achieve it? Doing researches i never came across to code which can parse json object keys except JSONObject.keys() which giving reverse order.
There is no way to achieve what you want with a JSONObject because JSONObjects are not ordered by definition and you shouldn't rely on the insertion order. If you need something ordered you should look into JSONArray
I have noticed that although JSONArray gives the proper order, the order won't be kept in HashMap, if you're using it for saving the values.
Using LinkedHashMap solves the order issue.

can i store two or more values with same key using SharedPreferences in android?

can i store two or more values with same key using SharedPreferences in android? If no, please tell me how to store values of username, first name, password etc when many users register in registration app?
Ex:
person A registered with username="john12", first name="john" and DOB="06/06/2000".
person B registered with username="arun89", first name="arun" and DOB="08/11/1989".
Now, I want to store these values in SharedPreferences and retrieve them later. Is it possible using SharedPreferences? If not, Please tell me how to do in other way.
Thank you in advance.
I woud consider creating a JSONObject and add the fields you want to store as a key:value pair.
json.putString(key, value);
You can then store the json object in it's string representation with json.toString() and restore it later with
JSONObject jo = new JSONObject(jsonString);
String value = jo.getString(key);
JSONObject also offeres different data types beside strings.
It really depends on how much data you want to store. Depending on that I would choose SharedPreferences or a SQLite implementation.
You cannot store these values directly (as ones added latter will overwrite previously added) but you can always store Parcelable and put your data into it
For your case it is better use SQLIte database.But if you want to use shared preference it is still possible.You have to use a key with additional index to remember different user like
UserName1:arun
UserName2:john
You have to remember the total number of user.Then can maintain all of them.you can also use other data structure like hashmap to maintain data for the shared preference.
I dont't think it is possible, as you don't know the number of users.
You could try to separate the users with commas, but that's lame.
You should consider using SQLite database.
http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
Then you have to store a array List of user objects first create a class userInfo then create a array List type of userInfo then store the data in this list and put a serialize-able object in SharedPreferences.
You can also store them on a single key called "registers" as string. Concatenate each register to preference. Put ";" (or any other characther you want) between each register. Then parse the string and use the values.
Key: registers
Value: "username=john12, first name=john, DOB=06/06/2000;username=mike12, first name=mike, DOB=06/07/2012"
Using split method of String will give you a list of registers as String.
registers.split(";");
Splitting again with "," will give you properties of each register.

Comparing JSONArrays

I am currently using a custom service in my android application to retrieve a JSONArray from a remote database. Once I have retrieved the JSONArray, I would like to compare its contents to another JSONArray which I call 'mostRecent.' If the newly retrieved JSONArray differs from 'mostRecent' then I would like to update 'mostRecent' with the newly retrieved JSONArray. If I am not mistaken, however, the JSONArray equals method compares the instance of the JSONArray object and not the contents, correct? Is there an easy method to compare the contents of two JSONArrays? Thanks for the help!
Use that:
array1.toString().equals(array2.toString());
Maybe you will be able to use even the strings you receive without constructing the JSONArrays, but then you will need to make sure the two are formatted the same way.

Categories

Resources