I am working with an api where I am retrieving JSON data. I've come across a data type that I am unfamiliar with and I would like some insight into what it means.
An example is shown below.
{
"data":{
"id":"92",
"name":"harry",
"friends":"a:2:{i:0;s:1:\"1\";i:1;s:2:\"15\";}"
"enemies":"a:0:{}"
},
"error":false
}
I am unsure how to interpret the "friends" and "enemies" fields. I am aware that arrays are represented by [ ] and objects by { }.
Could anyone explain what they mean?
Thank you.
The fields contain arrays that have been serialized by PHP.
For example
<?php
$friends = "a:2:{i:0;s:1:\"1\";i:1;s:2:\"15\";}";
$arr = unserialize($friends);
var_dump($arr);
?>
gives
array(2) {
[0]=> string(1) "1"
[1]=> string(2) "15"
}
What these values represent will be application-specific.
The value of friends is a string with the value: a:2:{i:0;s:1:"1";i:1;s:2:"15";}
What this string represents depends on what the application does with it
Related
I am working on an module in which i have to get the json data and get array name (which is table name of android database) and all keys and value of array(which is keys=columns & values=data of columns) and array name(table name) and their related data can be more than 1 or in 20s or more . i do not know how to implement it because i have not done this kind of code before..please help me to solve this problem.
my json code is like.. every array name would be different this is an example only but data will coming like this
{
"MSG": "OK",
"data": [
{
"strPrimaryKey": "iDeviceAppId",
"Device_App": [
{
"strAppName": "NI Data Dashbard",
"isDeleted": "0",
"strVersion": "2.3.0",
"dtCreateDate": "2018-04-09",
"iDeviceAppId": "0",
"dtUpdateDate": "2018-04-13",
"iOnHomeScreen": "1"
}
]
}
]
}
You can parse the JSON you received into a map and then traverse the map to get what you need.
I'm no back-end developer. So perspective is always appreciated.
I have written a script which requests from an API and creates this huge JSON file I want to save in firebase, how can I accomplish this? And would it be possible to filter this json with python for example; when I add region=eu in the url this returns the objects which have Europe as region or do I absolutely need to request the entire json file and parse in my code (java android) ?
Since there are a few parts to your question:
You can save JSON to Firebase and the data will be mapped to child locations:
Using PUT, we can write a string, number, boolean, array or any JSON object to our Firebase database...When a JSON object is saved to the database, the object properties are automatically mapped to child locations in a nested fashion.
https://firebase.google.com/docs/database/rest/save-data
And for your next question:
And would it be possible to filter this json with python for example; when I add region=eu in the url this returns the objects which have Europe as region
Looks like you should be able to jimmy something together with Firebase's filters, startAt and endAt:
We can combine startAt and endAt to limit both ends of our query.
https://firebase.google.com/docs/database/rest/retrieve-data#section-rest-filtering
For your example you might do something like this:
curl 'https://yourfirebase.firebaseio.com/yourendpoint.json?orderBy="$REGION_NAME"&startAt="EU"&endAt="EU"&print=pretty'
...or do I absolutely need to request the entire json file and parse in my code (java android) ?
The facts that JSON objects are stored hierarchically in Firebase and that you can filter based on those object values makes me think you do not, in fact, have to request the entire JSON file. However, I don't have personal experience with this particular aspect of Firebase, so give it a shot!
As #ackushiw mentions in the comments, you can also use the equalTo query (https://firebase.google.com/docs/reference/js/firebase.database.Query#equalTo):
curl 'https://yourfirebase.firebaseio.com/yourendpoint.json?orderBy="$REGION_NAME"&equalTo="EU"&print=pretty'
It really depends on how you are structuring your JSON. It's generally recommended to make your JSON tree as shallow as possible since all children are loaded when you have a matching query.
FIREBASE DATA:
{
"-id1": {
"region": "eu" // bear in mind queries are case sensitive
"title": "Foo"
"nested": {
"city": "berlin"
}
},
"-id2": {
"region": "other"
"title": "Bar"
"nested": {
"city": "berlin"
}
},
"-id3": {
"region": "eu"
"title": "Baz"
"nested": {
"city": "paris"
}
}
}
Querying with (using the Android API)
.orderByChild("region").equalTo("eu")
would return "-id1" and "-id3"
with
.orderByChild("nested/city").equalTo("berlin")
would return "-id1" and "-id2"
The REST API Returns Unsorted Results: JSON interpreters do not enforce any ordering on the result set. While orderBy can be used in combination with startAt, endAt, limitToFirst, or limitToLast to return a subset of the data, the returned results will not be sorted. Therefore, it may be necessary to manually sort the results if ordering is important.
If you're using a more complex structure I recommend watching this video
https://www.youtube.com/watch?v=vKqXSZLLnHA
I'd also recommend using the firebase library for Android
https://firebase.google.com/docs/android/setup
And Firebase-UI, It does a lot for you.
https://firebaseopensource.com/projects/firebase/firebaseui-android/
I get a server response like this:
{
"action":"add",
"domain":"dm1",
"params":{
"add1":"v1",
"add2":"v2",
"add3":"v3"
}
}
And i have many domain (15) and each domain have multi action, and key in value of "params" key ("add1", "add2",...) that are depended on value of action value ("add", "remove", ...). So, i want to ask that, how can i get param value from server then push in a Model using Gson. I'm Android.
Thanks in advance!
You can parse your params key as a JSONObject and iterate through all its keys to get the values of those dynamic keys. Use JSONObject.keys to get an Iterator for keys. This answer should further help you - How to parse a dynamic JSON key in a Nested JSON result?
I am not pretty sure. What your goal is.
But You can do as below
{
"action":"add",
"domain":"dm1",
"params":[
{ "add":"v1" },
{ "add":"v2" },
{ "add":"v3" }
]
}
I'm new to Android. I have learnt some basic concepts in Android. Now I'm learning JSON, I wanted to know the definitions of JSONArray,JSONObject,JSONStringer and JSONTokenizer. I'm a bit confused with these terms.Can anyone provide me the correct definition for these terms??
Thanks
json array:
[
{
"id":711
}, {
"id":712
}
]
json object:
{
"id":711
}
1) Array([)
In a JSON file , square bracket ([) represents a JSON array.
2) Objects({)
In a JSON file, curly bracket ({) represents a JSON object.
3) Key
A JSON object contains a key that is just a string. Pairs of key/value make up a JSON object.
4) Value
Each key has a value that could be string , integer or double e.t.c
see more detailed explanation here:http://www.tutorialspoint.com/android/android_json_parser.htm
That is, can you send
{
"registration_ids": ["whatever", ...],
"data": {
"foo": {
"bar": {
"baz": [42]
}
}
}
}
or is the "data" member of the GCM request restricted to one level of key-value pairs? I ask b/c that limitation is suggested by the wording in Google's doc[1], where it says "data" is:
A JSON object whose fields represents the key-value pairs of the message's payload data. If present, the payload data it will be included in the Intent as application data, with the key being the extra's name. For instance, "data":{"score":"3x1"} would result in an intent extra named score whose value is the string 3x1 There is no limit on the number of key/value pairs, though there is a limit on the total size of the message. Optional.
[1] http://developer.android.com/guide/google/gcm/gcm.html#request
Just did a test myself and confirmed my conjecture.
Send a GCM to myself with this payload:
{
"registration_ids": ["whatever", ...],
"data": {
"message": {
"bar": {
"baz": [42]
}
}
}
}
And my client received it and parse the 'message' intent extra as this:
handleMessage - message={ "bar": { "baz": [42] } }
So the you can indeed do further JSON parsing on the value of a data key.
Although it appears to work (see other answers and comments), without a clear statement from Google, i would not recommend relying on it as their documentation consistently refers to the top-level members of the json as "key-value pairs". The server-side helper jar they provide [1] also reinforces this idea, as it models the user data as a Map<String, String>. Their Message.Builder.addData method doesn't even support non-string values, so even though booleans, numbers, and null are representable in json, i'd be cautious using those, too.
If Google updates their backend code in a way that breaks this (arguably-unsupported) usage, apps that relied on it would need an update to continue to work. In order to be safe, i'm going to be using a single key-value pair whose value is a json-stringified deep object [2]. My data isn't very big, and i can afford the json-inside-json overhead, but ymmv. Also, one of my members is a variable-length list, and flattening those to key-value pairs is always ugly :)
[1] http://developer.android.com/guide/google/gcm/server-javadoc/index.html (The jar itself is only available from within the Android SDK in the gcm-server/dist directory, per http://developer.android.com/guide/google/gcm/gs.html#server-app )
[2] e.g. my whole payload will look something like this:
{
"registration_ids": ["whatever", ...],
"data": {
"player": "{\"score\": 1234, \"new_achievements\": [\"hot foot\", \"nimble\"]}"
}
}
Scuse me if I'm wrong, Map<String, String> denotes key=string and value=string.
If string is a long unreasonable json extract which is UTF-8 formatted and well escaped. It stands to reason that should you call new JSONObject(receivedString); and it works then all other json calls follow.
Do not forget that raw JSON is a string! We do not need google to clarify how to work with strings..this is why your test worked!