How to deserialize this object from json to string? - json

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()

Related

Get JSON collection inside another JSON in VB.NET

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

Generation of dynamic class from XML to JSON

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

What is the correct way to deserialize?

hello im saving a php array in .txt file using json_encode, next i need use this array in vb.net, but really i can do a while or for each,
this is the array string
"[{"Id_Event":"5713616","Deporte":"Soccer","Pais":"Austria Amateur","Liga":"Regionalliga East","Jornada":"20","Local":"SKU Amstetten","Visita":"Wiener SC Axa","Fecha_Evento":"2015-03-18T19:30:00","encuentros":[{"Items":[{"Por":"1","Fila":"3way15713616","Cuota":"1.9","$$hashKey":"019"}],"Tipo":"3way","Gan_Max":1.9,"$$hashKey":"017"}],"$$hashKey":"015"},{"Id_Event":"6804834","Deporte":"Soccer","Pais":"England Amateur","Liga":"Southern Football League,","Jornada":"1","Local":"Cambridge City","Visita":"Frome Town","Fecha_Evento":"2015-03-18T20:45:00","encuentros":[{"Items":[{"Por":"1","Fila":"3way16804834","Cuota":"1.7","$$hashKey":"01F"}],"Tipo":"3way","Gan_Max":1.7,"$$hashKey":"01D"}],"$$hashKey":"01B"}]"
in the project i have the Imports Newtonsoft.Json.Linq
please help me
You can deserialize your json to a List(Of JObject) for the simplest way to be able to iterate through the json array :
Dim jsonString As String = "your_json_text"
Dim json As List(Of JObject) = JsonConvert.DeserializeObject(Of List(Of JObject))(jsonString)
For Each jObject As JObject In json
Console.WriteLine(jObject)
Next
Better way would involve creating a class to map each item in your json array, then deserialize your json to List(Of YourClass) instead of List(Of JObject).

Json.NET convert linq result into IEnumerable<JObject>

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))

Newtonsoft json exception

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