Json formation Getting corrupted - android

I am using JSONObject to create a new json in my Android Application.
But I am experiencing a strange issue where I am observing that my json field names are getting replaced by letters like "a" : "value " , "b " : "value_1" , "c" : value2 "
This works fine for smaller number of childs but as the number grows it distorts the json :
example json :
{
"Employees" : [
{
"userId":"rirani",
"jobTitleName":"Developer",
"firstName":"Romin",
"lastName":"Irani",
"preferredFullName":"Romin Irani",
"employeeCode":"E1",
"region":"CA",
"phoneNumber":"408-1234567",
"emailAddress":"romin.k.irani#gmail.com"
},
{
"userId":"nirani",
"jobTitleName":"Developer",
"firstName":"Neil",
"lastName":"Irani",
"preferredFullName":"Neil Irani",
"employeeCode":"E2",
"region":"CA",
"phoneNumber":"408-1111111",
"emailAddress":"neilrirani#gmail.com"
},
{
"userId":"thanks",
"jobTitleName":"Program Directory",
"firstName":"Tom",
"lastName":"Hanks",
"preferredFullName":"Tom Hanks",
"employeeCode":"E3",
"region":"CA",
"phoneNumber":"408-2222222",
"emailAddress":"tomhanks#gmail.com"
}
]
}
Corrupted output :
{
"Employees" : [
{
"a":"rirani",
"b":"Developer",
"c":"Romin",
"d":"Irani",
"e":"Romin Irani",
"f":"E1",
"g":"CA",
"h":"408-1234567",
"i":"romin.k.irani#gmail.com"
},
{
"a":"nirani",
"b":"Developer",
"c":"Neil",
"d":"Irani",
"e":"Neil Irani",
"f":"E2",
"g":"CA",
"h":"408-1111111",
"i":"neilrirani#gmail.com"
},
{
"a":"thanks",
"b":"Program Directory",
"c":"Tom",
"d":"Hanks",
"e":"Tom Hanks",
"f":"E3",
"g":"CA",
"h":"408-2222222",
"i":"tomhanks#gmail.com"
}
]
}
Is this a bug in the Android JSON object ? Any hints?

this issue based on proguard rules in app level gradle file to that value make false like below ..
minifyEnabled false
other wise you can add key in your pojo class like
#SerializedName("userId") // pass your json key
private String userId;

Related

How to retrieve the list of all GitHub repositories of a person? parsing json list with no key value

I know this question has been answered at this post with same question. however, my question is the Json response by the most upvoted answer will be a json list with no key value.
(you can also check the sample json from the official github website)
I am using Moshi library to parse json. However, I have no idea how to parse that Json list whose key value is not set.(only a list present in the Json with no Key value for that list)
this is what it looks like though
[
{
"id": 1296269,
"node_id": "MDEwOlJlcG9zaXRvcnkxMjk2MjY5",
"...": "...",
...
},
{
"id": 1296255,
"node_id": "somevalue",
"...": "...",
}
...
]
since the purpose of asking this question is to get a list of repositories of a user, you could leave any code snippet to get that type of list Json to Kotlin data class.
I appreciate your help in advance.
Try this one for Array list
inline fun <reified T> Any?.getResponseInArrayList(): ArrayList<T?>? {
return try {
val token: TypeToken<ArrayList<T>> = object : TypeToken<ArrayList<T>>() {}
val json = Gson().toJson(this)
Gson().fromJson(json, token.type)
} catch (e: Exception) {
Log.e("GSON ERROR ", "" + e.message)
null
}
}
use like this
val model = yourJsonString.getResponseInArrayList<YouJsonModel>()

Firebase Android - updateChildren owerwrites key-values in nested JSON structure

I've tried to find an answer about the following problem - found a couple in similar questions but cannot still solve it. If I flatten the structure it seems to work, but why?
basically - how do I add more key-value pairs in a json-object at Firebase WITHOUT owervriting existing ones?
let say I have the following jsonstructure
"groups" : {
"aw" : {
"members" : {
"Lisa" : "true"
},
"name" : "aw university"
}
}
How do I then add more key-value pairs in members without overwriting existing ones? For instance, I want to add one more member - Magnus so the object becomes:
"groups" : {
"aw" : {
"members" : {
"Lisa" : "true",
"Magnus" : "true"
},
"name" : "aw university"
}
}
I have tried with updateChildren without success:
var hm = HashMap<String, Any>()
hm.put("magnus", "true");
database.child("groups").child("aw").child("members").updateChildren(hm)
What happends is that existing key-value (Lisa: true) is overwritten
Is there a simple solution to this problem?
the strange thing is that if the structure is flattened - updateChildren works and adds Magnus
"groups" : {
"aw" : {
"name" : "aw university"
},
"members" : {
"Lisa" : "true",
"Magnus" : "true"
}
}
But I want "members" to be a child of "aw"
Try this database.child("groups").child("aw").child("members").setValue(hm);

How To Parse Response of Multiple Types of Single Key

How To Parse Response of Multiple Types?
Key is like (suppose student_list is a key of list types when student_list is empty then it makes as a string like student_list=""), How to manage this types of response using Retrofit? I am using MVVM Model with retrofit.
My Response:
when I get Data into the List
{
"status": 200,
"data": [
{
"prod_month_total": 2989.61,
"product": "GAS"
},
{
"prod_month_total": 39566.22,
"product": "OIL"
},
{
"prod_month_total": 83912.55,
"product": "OTHER"
}
]
}
when List is Empty Then Response:
{"status":404,"data":"No result found"}
I am getting this Error:
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 23 path $.data
first create the right model calss use this site http://www.jsonschema2pojo.org
than use
`if( reponce.isSucessful){
if(responce.status==200){
//code here
}
else{
// find the error
}
}else
{
//
}`

Firebase database is being updated at one node but is not affecting the other node

I have two siblings namely "intents" and "messages". when I update the messages fields it gets done properly but one the same procedure is done with intents fields, no change occurs. Below is the android-java code for the same.
IntentMessage testIntentMessage = new IntentMessage("Hello test intent", "Hello test intent");
FriendlyMessage tempMessage = new FriendlyMessage("namastey", "india", "http://imgsdown.1mobile.com/group1/M00/C1/21/S36rZla-KYqAV08jAABUIgbzI-E596.png","543587653");
mFirebaseDatabaseReference.child("intents").child("-sjhfdvshv").setValue(testIntentMessage);
mFirebaseDatabaseReference.child("messages").child("-KUVR-MkLOVwNkx6nm-_").setValue(tempMessage);
Below is my firebase database JSON file after above commands are executed:
{
"intents" : {
"-kjuvhsjkv" : {
"intentFields" : "testIntentFields 1",
"intentName" : "testIntentName 2"
},
"-sjhfdvshv" : {
"intentFields" : "testIntentFields",
"intentName" : "testIntentName"
}
},
"messages" : {
"-KUVR-MkLOVwNkx6nm-_" : {
"name" : "india",
"photoUrl" : "http://imgsdown.1mobile.com/group1/M00/C1/21/S36rZla-KYqAV08jAABUIgbzI-E596.png",
"text" : "namastey",
"timeStamp" : "543587653"
}
}
}
}
The real problem is :
when everything is alright with "messages" node, why is the "intents" node not being updated?
Given that I have made the IntentMessage class in the same way as FriendlyMessage class
I got my problem as commented by Wilik, I didn't mention the rules for "Intents" section in the "rules" section of the database. Now I'm getting proper data.

Laravel display results of an array as single json object

Is there a way I can get my response to be a single object with an array of users using Eloquent?
For instance:
{
"results" : [
{ "email" : "test1#test.ca" },
{ "email" : "test2#test.ca" }
]
}
Current it outputs like this:
[
{
"email": "test1#test.ca",
},
{
"email": "test2#test.ca",
}
]
This is how I'm displaying users from my code:
$users = User::whereIn('number', $numbers)->select('email')->get();
return $users;
Which would be fine but I'm using Volley for Android using JSONObjectRequest but its failing when it tries to parse the JSON because it can't parse the array.
You can try it like this:
$users = User::whereIn('number', $numbers)->select('email')->get();
return Response::json(array('results' => $users));

Categories

Resources