I have a JSON object de-serialized using Newtonsoft JSON in VB.NET How can I get a specific key type value when it has a JSON inside another JSON. Please see JSON response below
I want to get the identity status based on identity type
My code attempt:
Dim jsonResult As String = myReader.ReadToEnd() 'My json line above
Dim jsonObject = JsonConvert.DeserializeObject(jsonResult)
Dim ContactNum = jsonObject("owner")("identity_statuses")("identity_status")
For i As Integer = 0 To ContactNum.Count
Dim arr = ContactNum(i)
Next
Related
I am trying to deserialize this object from json to string but it doesnt work.
[\"661253BF9FE5463D968AC4CF9179FC56\"] -- this is the object.
Dim idDes As String = JsonConvert.DeserializeObject(id)
I want it like this: "661253BF9FE5463D968AC4CF9179FC56"
Any help?
What your JSON is telling you is that it's an array of strings, that's what you need to deserialize and if you always know you want the first string in that array you can access like this:
Dim idDes As String
Dim arr As String() = JsonConvert.DeserializeObject(Of String())(json)
idDes = arr.First()
What I am trying to achieve is convert XML to a JSON object. Currently I am doing it like this:
Public Class Person
Public Property Name As String
' other properties here'
End Class
Dim doc As XmlDocument
doc.LoadXml(arg_strXml)
Dim jsonValue As String = JsonConvert.SerializeXmlNode(doc)
Dim jsonObject = JsonConvert.DeserializeObject(Of Person)(jsonValue)
Dim firstName As String = jsonObject.Name
However the issue is the retrieved XML, and thus the deserialized JSON object has different fields/properties/elements depending on the correct function. It would be a nightmare to have a class for each possible XML.
Is there a way round not having to create a specific class (Person in this case) for each deserialize?
You can deserialze/parse your JSON string into Newtonsoft's JObject. Then you can access the properties like Dictionary(Of String, String), for example :
Dim arg_strXml = "<Person><Name>foo</Name></Person>"
Dim doc = New XmlDocument()
doc.LoadXml(arg_strXml)
Dim jsonValue = JsonConvert.SerializeXmlNode(doc)
Dim jsonObject = JObject.Parse(jsonValue)
Console.WriteLine(jsonObject("Person")("Name"))
dotnetfiddle demo
output :
foo
I have a LINQ object that needs to be converted into IEnumerable JObject
The following JavaScriptSerializer is working fine in that I can see the json data from the LINQ object.
Dim serializer As New JavaScriptSerializer()
Dim serializedResult = serializer.Serialize(results)
I need to do something similar with the JSON.NET but I have tried...
Dim jobject = New JObject(results)
Dim jarray = New JArray(results)
both have the following error. "Could not determine JSON object type for type Services.Models.Log"
I have tried this
Dim jobject = JObject.FromObject(results)
with the following error. "Object serialized to Array. JObject instance expected."
The only code that seems to work is this.
Dim jtoken = JToken.FromObject(results)
but how to I turn the JToken back into a JObject so that I can return it as IEnumerable?
How about this way :
Dim jobjects As IEnumerable(Of JObject) =
results.Select(Function(x) JObject.FromObject(x))
I am currently making a program that parses the Urban Dictionary API but I cannot get it to return the selected definition.
This is my current code fore retrieving and parsing the data:
Dim sourceString As String = New System.Net.WebClient().DownloadString("http://api.urbandictionary.com/v0/define?term=" & strRet)
rtxtDefinition.Text = sourceString
Dim jResults As JArray = JArray.Parse(sourceString)
Dim results As List(Of JToken) = jResults.Children().ToList()
For Each item As JProperty In results
item.CreateReader()
MsgBox(item.Value("definition"))
Next
note that strRet is the users input
this is an example of the urban dictionary API structure: http://pastebin.com/11Z5uVRN
The current code does not have support to find the (n)th definition only because I first need to get it to return a definition.
So obviously I am doing something wrong because of the error: Newtonsoft.Json.JsonReaderException but I am not sure.
Any help would be amazing. Thanks!
EDIT:
Root of json string you're dealing with is not an array but single object. Therefore, you can parse it to JObject instead of JArray, for example :
Dim jobj As JObject = JObject.Parse(sourceString);
Dim arr As JArray = jobj("list");
For Each(var item in arr.Children(Of JObject)())
MsgBox(item("definition").ToString());
Next
I have the following code:
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Module Module1
Structure JSONList
Dim Name, Email As String
Dim Age As Integer
End Structure
Sub Main()
Dim Data(1) As JSONList
Data(0).Name = "Josh"
Data(0).Age = 17
Data(0).Email = "me#mail.co.uk"
Data(1).Name = "Greg"
Data(1).Age = 17
Data(1).Email = "greg#hotmail.co.uk"
Dim JSONEncode As String
JSONEncode = JsonConvert.SerializeObject(Data)
Console.WriteLine(JSONEncode)
Console.WriteLine()
Console.WriteLine()
Dim JSONDecode() As JSONList = JsonConvert.DeserializeObject(JSONEncode)
Console.WriteLine(JSONDecode(0).Name)
Console.ReadKey()
End Sub
End Module
The first encoding part of the script is used to store the encoded string to a database, the output is:
[{"Name":"Josh","Email":"me#mail.co.uk","Age":17},{"Name":"Greg","Email":"greg#hotmail.co.uk","Age":17}]
Now when I try to decode this JSON string, I get an error Unable to cast object of type 'Newtonsoft.Json.Linq.JArray' to type 'JSONList[]'.
I need the data to be encoded in the JSON format so that I can use it in my website that uses PHP to decode it. I am using Visual Basic 2010 along with JSON.NET.
The problem is that JsonConvert.DeserializeObject deserialises into an object of type Newtonsoft.Json.Linq.JArray which .net cannot automatically convert to an array of JSONList. A little bit of conversion is required:
Dim jsonObject As Newtonsoft.Json.Linq.JArray =
JsonConvert.DeserializeObject(JSONEncode)
Dim JSONDecode() As JSONList = (
From j In jsonObject
Select New JSONList() With {.Age = j("Age"),
.Email = j("Email"),
.Name = j("Name")}
).ToArray()
As #Adrian said, JsonConvert.DeserializeObject will deserialize to JArray, which .Net cannot automatically convert into an array of your JSONList structure. However, you don't need to do manual conversion; you simply need to use the overload of DeserializeObject which accepts a type parameter. This will allow Json.Net to deserialize directly to your type without needing any special conversion code.
Dim JSONDecode() As JSONList = _
JsonConvert.DeserializeObject(Of JSONList())(JSONEncode)