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)
Related
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.
I have an iterable of People that I save as a string after converting from json. I want to know how would I convert the string back to a list.
// Save data
val peopleString = myList.toString()
// String saved is
[People(name=john, age=23), People(name=mary, age=21), People(name=george, age=11)]
Now is it possible to convert peopleString back to a list?
val peopleList: List<People> = peopleString.?
In short, no... kind of.
Your output is not JSON, and toString() is the wrong function to use if you wanted JSON. The output of toString() is not a proper serialization format that can be understood and used to rebuild the original data structure.
Converting a data structure into some format so that it can be transmitted and later rebuilt is known as serialization. Kotlin has a serializer which can serialize objects into a number of different formats, including JSON: https://github.com/Kotlin/kotlinx.serialization#quick-example.
It's not as easy to use as toString(), but that's to be expected as toStrings's purpose is very different from serialization.
I have an object that has custom data types in it. When I try to pass that object through my REST API, I get an error that says: The 'ObjectContent'1' type failed to serialize the response body for content type 'application/json; charset=utf-8'. I am assuming that the API is attempting to serialize my custom object (with custom data types) in order to convert it to JSON. Is there a way I can make this possible? I am coding in visual basic.
EDIT: I am using ASP.NET Framework 4.6.1. My object is involved with connecting to a database. Because of that, I have created custom datatypes such as databasePointer which is simply a long value. This is so I don't mix up numbers when connecting to the database. I simply need to find out how to convert those custom data types back to primitive data types before I pass my object to be serialized.
I ended up simply taking the object, running an iteration of properties (that I chose so they were primitive values), and copying them over to another object. I then passed that through and it worked fine. I had to write a class to make the conversion for the specific object, but it works well and didn't take long.
Example class:
Public Class Converter
Dim propertyKeys As New ArrayList
Private Sub InitiateKeys()
'add keys here for property names
End Sub
Public Function convertToString(ByVal order As ShopOrder) As
InitiateKeys()
Dim json As ShopOrderJSON = New ShopOrderJSON
If (IsDBNull(order)) Then
Return json
End If
Try
For Each key As String In shopOrderKeys
json.GetType.GetField(key).SetValue(json, order.GetType.GetField(key).GetValue(order).ToString)
Next
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Return json
End Function
End Class
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 know returning types in a wcv service is allowed, and it will convert the object to json. But what if I don't want to return a random type, but return a string with formatted json? I can construct json my self but it can a) be messy, and b) not auto encode html values.
How do I do build a custom formatted json string? Off the top of my had, can I return a dictionary with key, value pairs? Will the value be encoded so you can transmitted without running the risk of malformed json?
Have a look at JSON.Net. I've used it in the past for serializing/deserializing to/from Json. It also (according to the web page) has support for converting Json to/from XML, so it seems reasonable that there would be some functions in there to build arbitrary Json strings in a way that is less error-prone than doing it yourself.
You can specify a return type of object and then use an anonymous type to return an arbitrary object. If you want to return an arbitrary collection, you can return IEnumerable, and use that to return a collection of anonymous types.
as far as I can understand, you want a webservice that returns a string that can be parsed using json (like JSON.parse(yourReturnedString)... As ckramer answered, JSON.NET can help to format your whatever dictionary into json but you should know dictionary is "json-serialised" as key:'your key', value:'your value§that can be also an object that will be serialized', so if you are using JSON.NET, you should also once it has been deserialezed, remove all the "key": and ,"value" JSON.NET returned.
so good so far you should definetely declare your webmethod as a method that returns a JSON format.
hope you found a solution before this answer...