Angel02
asked on
JSON response syntax
I receive a JSON response in the following syntax, where it is returned as objects and not array. Is this right?
{
"status": "OK",
"product":
{
"name": "ABC",
"code": "A",
"Price": "2.5",
"active": true,
"contact":
{
"name": "X",
"phone": "999-999-9999",
"email": "X@company.com"
}
}
}
I use the following code to access it and it throws error. Object serialized to Object. JArray instance expected.
This code would work if the "product" was returned as an array.
Dim parsedObject = JObject.Parse(response)
Dim docs = JArray.FromObject(parsedOb ject("prod uct")).Sel ect(Functi on(x) x.ToObject(Of Product)())
Dim productDetail = docs.Select(Function(x) x).OrderBy(Function(x) x.name).ToList
Public Class Product
Public Property name As String
Public Property code As String
Public Property price As Decimal
Public Property active As Boolean
End Class
Do I need to access the JSON response differently or is the response syntax wrong?
{
"status": "OK",
"product":
{
"name": "ABC",
"code": "A",
"Price": "2.5",
"active": true,
"contact":
{
"name": "X",
"phone": "999-999-9999",
"email": "X@company.com"
}
}
}
I use the following code to access it and it throws error. Object serialized to Object. JArray instance expected.
This code would work if the "product" was returned as an array.
Dim parsedObject = JObject.Parse(response)
Dim docs = JArray.FromObject(parsedOb
Dim productDetail = docs.Select(Function(x) x).OrderBy(Function(x) x.name).ToList
Public Class Product
Public Property name As String
Public Property code As String
Public Property price As Decimal
Public Property active As Boolean
End Class
Do I need to access the JSON response differently or is the response syntax wrong?
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Got it. Thanks!
Open in new window
Example -
Open in new window
Produces the following output -In the future, you can use http://json2csharp.com/ in order to generate c# classes. Translating these classes into VB.NET is relatively straigthforward; e.g. the classes from your presented json are generated as:
Open in new window
-saige-