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).
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()
I am creating vb.net application. I am getting Json data from google finance. I am facing a problem in parsing. The problem is that :
I will give an example (not about google)
This is the class
Public Class MyModel
Dim m_tes As String
Dim m_client_list As String
Public Property type() As String
Get
Return m_tes
End Get
Set(ByVal value As String)
m_tes = value
End Set
End Property
Public Property client_list() As String
Get
Return m_client_list
End Get
Set(ByVal value As String)
m_client_list = value
End Set
End Property
End Class
and this is the JSON Deserializer
Dim deserializedProduct As MyModel = JsonConvert.DeserializeObject(Of MyModel)(JSON)
MsgBox(deserializedProduct.type)
MsgBox(deserializedProduct.client_list)
If I get one record Json data ,It works fine
like
dim JSON = {"type":"newModel","client_list":"Joe"}
The output of the msgbox is
newModel
Joe
The problem is that if I get a list of Json
I need a way to split this list likeh the following:
Json = {"type":"clientlist","client_list":"client 1"},{"type":"clientlist","client_list":"client 1"}
I haven't worked a whole lot with JSON, but I think you can pass a collection into the deserializer to get a back a collection of the deserialized objects.
In other words:
Dim deserializedProduct As List(Of MyModel) = JsonConvert.DeserializeObject(Of List(Of MyModel))(JSON)
The above code is similar to what you posted, except that is uses List<T> instead of a single instance of your MyModel object, and for the type used with the DeserializeObject you give it a List(Of ModelType).
Note that your MsgBox(deserializedProduct.type) and MsgBox(deserializedProduct.client_list) won't show you the results in a List<T> (you'll get the type name instead) - you'll need to loop through the list. Something like this:
For Each Dim model As MyModel In deserializedProduct
MsgBox(model.type)
MsgBox(model.client_list)
Next
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)