How to deserialize json to class with missing fields - json

I'm following a tutorial where one of the tasks is to deserialize json using gson. I looked at several SO posts, but couldn't find one that addressed my issue. I get a string representation of an array in JSON whose fields look like below:
photoJsonArray
[{"id": "28857102437",
"owner":"9457266#N02",
"secret":"a5f02e005f",
"server":"857",
"farm":1,
"title":"",
"ispublic":1,
"isfriend":0,
"isfamily":0,
"url_s":"https:\/\/farm1.staticflickr.com\/857\/28857102437_a5f02e005f_m.jpg" ...and so on
I want to deserialize this list into a list of GalleryItem objects, which only includes the id,url_s and id fields from the JSON array.
I tried the following approach to converting the JSON string to a list of Gallery Item objects, but all the fields ended up with a null value.
Type photoType = new TypeToken<List<GalleryItem>>(){}.getType();
List<GalleryItem>galleryList = g.fromJson(photoJsonArray.toString(),photoType);
I would really appreciate if someone could point me in the right direction!

after some research I discovered that all the fields from the JSON string need to be declared in the class in order to deserialize the JSON into that class. Seems a bit overkill if all you need to use is a couple fields, but hope this helps.

Related

Newtonsoft.Json.JsonSerializationException - Cannot deserialize the current JSON object

Lot of similar questions but still not able to make it. Here is my
code in vb.net and this is the json response. enter image description here I know this is because of [] but i'm using list don't know still getting the same error.
This is because you are receiving a JSON array and not a single JSON object.
You need to deserialize the array like this -
Dim Items_Array = Newtonsoft.Json.JsonConvert.DeserializeObject(Of T())(jsonString)

Parsing Complex JSON in Scala

I have the below JSON field in my data.
val myjsonString = """[{"A":[{"myName":"Sheldon""age":30"Qualification":"Btech"}]"B":"UnitedStates"},{"A":[{"myName":"Raj""age":35"Qualification":"BSC"}]"B":"UnitedKIngDom"},{"A":[{"myName":"Howard""age":40"Qualification":"MTECH"}]"B":"Australia"}] """
The parse method gives the following structure:
scala > val json = parse(myjsonString)
json: org.json4s.JValue = JArray(List(JObject(List((A,JArray(List(JObject(List((myName,JString(Sheldon)), (age,JInt(30)), (Qualification,JString(Btech))))))), (B,JString(UnitedStates)))), JObject(List((A,JArray(List(JObject(List((myName,JString(Raj)), (age,JInt(35)), (Qualification,JString(BSC))))))), (B,JString(UnitedKIngDom)))), JObject(List((A,JArray(List(JObject(List((myName,JString(Howard)), (age,JInt(40)), (Qualification,JString(MTECH))))))), (B,JString(Australia))))))
I am trying to parse it using Scala json4s. Visited almost all previously asked questions related to this, however, could not get proper solution to this. The O/P should be something like this:-
UnitedStates 30
UnitedKIngDom 35
Australia 40
or only the age in 30#35#45 format.
The JSON you posted is invalid, there are missing commas between your object fields.
In order to get the output that you want you will need to extract the data from the parsed AST that Json4s will create upon successful parsing of the data. Json4s provides a number of ways with which you can manipulate and extract data from a parsed AST.
You could map over the list of objects inside the JArray and extract the country and age from each object. I don't wish to provide code to do this, as you haven't provided an example of what you have tried to do other than simply parsing the JSON string.

POST JSON string from Form in VB.NET

Here is my situation:
I have a form that is collecting a list of items in a textarea as a JSON Object.
The form textarea looks like this:
<textarea id="listItems">
[
{"id":"1","name":"apple"},
{"id":"2","name":"orange"},
{"id":"3","name":"banana"}
]
</textarea>
I need to be able to parse that string and POST each item into a SQL Table.
ItemID | ItemName
-----------------
1 | apple
2 | orange
3 | banana
I don't think I have a good understanding of using the JavaScriptSerializer Class. I'm using VB.net
I don't have any server-side code yet but I know that I have to parse out the JSON string and then loop through it and then save each item.
Couldn't I just convert the JSON string into a DataTable and then loop through that temporary table?
Not sure. I'm trying to figure this out but some help would be useful.
Also I'm referencing a few SO posts to see if maybe I can figure this out
This one
This one
So expecting the following Json Object at the Server
[
{"id":"1","name":"apple"},
{"id":"2","name":"orange"},
{"id":"3","name":"banana"}
]
you would be able to do this:
Public Function DoSomething()
Dim jsonObject = "[{""id"":""1"",""name"":""apple""},{""id"":""2"",""name"":""orange""},{""id"":""3"",""name"":""banana""}]"
Dim obj = New JavaScriptSerializer().Deserialize(Of TestObject())(jsonObject)
End Function
With this Model
Public Class TestObject
Public Property id As Integer
Public Property name As String
End Class
At runtime you will have an array of Objects in obj which you can then manipulate and fill into a DataTable or directly feed to the Database. Converting the Json into an Object is not the only option you have here but it is a convenient one.
Since you did not specify the context you are working in it is a little unclear what might be the best solution to your case. Maybe you want to do a little research on different Json Frameworks (for example Json.net) as well as Data-Handling with Databases so you get ideas about the how and when.
This was the best answer I found and I adjusted it to meet my needs.
I've also included the link to the duplicate POST that answered my question.
I'm also referencing another similar post which has a similar situation.
This is the link to the .Net Fiddle that demonstrates what I was looking for.
DEMO
How to deserialize JSON which can be an array or a single object
How to handle both a single item and an array for the same property using JSON.net

Using Jolt, How do I remove a field "last_update" from everywhere in a JSON string?

I have a JSON array of objects and in many of the objects, at various points, there's a "last_update" field. ("Person" object may have a "Jobs" array and each Job in Jobs array may have an last_update, as well as the parent Person, as well as each Address in the "Addresses" object, etc. The "last_updated" field is not always at the same depth for various objects and in some objects may appear in multiple places.
I want to remove any mention of "last_update" no matter where in the JSON tree it lands.
If I was editing the JSON in Vim, I'd probably try using something like s/last_updated.*?//g.
It does not have OOTB support for that.

How to convert RealmResult to Json using Gson library

In this question How can I serialize a RealmObject to JSON in Realm for Java? The realm representative said that one can serialize realm object through GSON. Can you please explain it how?
I tried this.
RealmResults<Dog> myDogs=realm.where(Dog.class).findAll();
new Gson().toJson(myDogs);
But StackOverflowError occurred.
To make GSON serialization work with Realm you will need to write a custom JsonSerializer for each object that can be serialized and register it as a TypeAdapter.
You can see an example in this gist: https://gist.github.com/cmelchior/ddac8efd018123a1e53a
You get StackOverflow becouse of Gson based on reflection but managed object (RealmObjectProxy) have no real fields and fields of parent is nulls also some of proxy fields produses recursion in field type recognition of Gson it happens in $GsonTypes class.
To serialize RealmObject you can use one of this options:
Write your own adapter for every RealmObject childs which will takes data using getters.
Call realm.copyFromRealm(realmObject) before serialisation. It will looks like new Gson().toJson(realm.copyFromRealm(realmObject))
Use library based on 2nd option RealmSupportForGson
Hope it helps
The easier way is create a List<Dog> with RLMResult<Dog>, and then serialise this List with Gson.
After two days of bug resolve, I found this simple solution:
YourRealmObject realmObj = realm.where(YourRealmObject.class).findFirst();
if(realmObj != null) {
realmObj = realm.copyFromRealm(realmObj); //detach from Realm, copy values to fields
String json = gson.toJson(realmObj);
}