I have a string object like this. I have tried deserializing using newtonsoft json converter but it results in the null object. I got to know the reason behind which is that the object I tried to convert to is inside the "data". Hence, it returns Null always for each property after conversion.
I want to know how I can access the "data" directly from this object?
{"data":{"providerRef":null,"orderId":"4579144","orderStatus":"x:app_pending","applicantInterfaceURL":"http://google.com","successful":true,"error":null,"reportAddress":null,"correlationId":"55f7022c-28f9-490a-8dd1-b30a40e3467a"},"status":0,"error":{"actionArguments":null,"errorCode":null,"errors":null,"message":null}}
Now, I have implemented it like
The class VolunteerBackgroundCheckResponse have these properties:
Can anyone help me getting through the data object only from the json string?
This is solved. I have used the JObject for the purpose and received all the properties beneath the main object. I accessed "data" and deserialized it.
Related
Is there an elegant way to convert an object that contains prototypes to an object with properties only? (all prototype methods would be omitted)
In one line:
_.omit(myObj, _.isFunction);
Also with JSON:
JSON.parse(JSON.stringify(myObj));
https://jsfiddle.net/yh2utkdw/1/
I have a Mongo collection annotated with #Document and I want the possibility to also get that Java object from a String (JSON) as we're getting these classes pushed into a queue as String.
Is there a method in Spring-Data-Mongo which converts from JSON to the actual Document object?
#Autowired
MongoTemplate mongoTemplate;
and then
mongoTemplate.getConverter().read(MatchMongo.class, (DBObject) JSON.parse(json));
Thanks to freakman, your answer helped a lot
You can try com.mongodb.util.JSON.parse() method. It returns object so you probably have to do the casting + it may be it need "class" field inside json string.
I wanna pass the json to server, below is the json format:
[{"StaffID":"S01","StaffRank":"Manager"},{"StaffID":"S02","StaffRank":"Waiter"}]
After I tried the following code to get the json array:
Dim request As String = New StreamReader(data).ReadToEnd
response = AddStaff(JsonConvert.DeserializeObject(Of tbl_Staff)(request))
Return JsonConvert.SerializeObject(response)
I get the new error which is:
"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type
'tbl_Staff' because the type requires a JSON object
(e.g.{"name":"value"}) to deserialize correctly. To fix this error
either change the JSON to a JSON object (e.g. {"name":"value"}) or
change the deserialized type to an array or a type that implements a
collection interface (e.g. ICollection, IList) like List that can
be deserialized from a JSON array. JsonArrayAttribute can also be
added to the type to force it to deserialize from a JSON array."
What is the problem? Thanks
The problem is you cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'tbl_Staff' because the type requires a JSON object (e.g.{"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
It seems like no matter how detailed I make that error message, people just don't read it :-\
I found the answer.I just change the code to List. Then working perfectly.
Dim request As String = New StreamReader(data).ReadToEnd
response = AddStaff(JsonConvert.DeserializeObject(Of List(Of tbl_Staff))(request))
Return JsonConvert.SerializeObject(response)
I've got a class that closely resembles one of my entities (I use the class for JSON de/serialization because the entity fails conversion to JSON, one of the known gotchyas of JSON + MVC).
Once I deserilize a JSON string into my object, is there a way to automatically update the associated Entity model instance. The property names are the same.
e.g.
'myDeserialized is the deserialized JSON object coming over the wire
Dim entityInstance As DLL.Person = db.getPersonById(myDeserialized.id)
myDeserialized.update(entityInstance)
Where this just goes through and takes all the properties in myDeserialized and updates the same named property in entityInstance?
Or is it possible to just iterate over a key-value pair of all the properties in an object/entity?
I'm looking for something like TryUpdateModel(), but that only works with forms, right? I don't want to tie my data to a form, that's all.
Thanks!
I don't know what type and version of EF you use, but there are very powerful functions for iterating on the entities properties. Which mean you can easily make your update function on the entity or its baseclass' partial (entityInstance.Update(myDesrialized)).
If you use EntityObject, then you can get an ObjectStateEntry for your entities and via this class you can very easily achieve this.
Please let us know what version and type of EF you use (POCO?)
Ok so I'm trying to use the JavaScriptSerializer to work with this code. However it crashes when it reaches the last line;
Dim json As New String(sr.Serialize(dt))
I get this error message;
A circular reference was detected
while serializing an object of type
'System.Reflection.Module'.
I would really appreciate any insights that could help solve this problem.
Circular reference means that serialising the object would result in an infinite loop.
For example if you would try to serialize object "A" having a 1 to 1 reference to object "B".
Declare a class containg the data you want to serialize with JSON to solve this issue.
As hkda150 has already said, you could use a class specifically tailored for being serialized.
This will furthermore enable you to have foreign key values serialized instead of having related full objects serialized. Thus, if you are serializing object a which has a property a.SomeB of type B, then you will often want the ID of a.someB to be present in your webpage. Obviously I don't know enough to be able to say if this is relevant in your specific use case.
BTW, If you find yourself doing a lot of mapping between "business objects" and "objects meant for serialization" you may want to consider using AutoMapper.