Here is my json like string:
{
"ProductGroupId": "3",
"ProductGroupName": "Frisdranken",
"ProductId": "139",
"ProductName": "Cola",
"Quantity": 1,
"QuantityUnit": "P",
"SellingPrice": 2.7,
"VatRateId": "A",
"DiscountLines": []
}, {
"ProductGroupId": "3",
"ProductGroupName": "Frisdranken",
"ProductId": "146",
"ProductName": "Plat water",
"Quantity": 1,
"QuantityUnit": "P",
"SellingPrice": 2.6,
"VatRateId": "A",
"DiscountLines": []
}
How do I get the "ProductName" and "Quantity" in a datatable?
Assuming your json is an array and you just missed the enclosing [...], you can:
Dim json As String = "[{""ProductGroupId"":""3"",""ProductGroupName"":""Frisdranken"",""ProductId"":""139"",""ProductName"":""Cola"",""Quantity"":1,""QuantityUnit"":""P"",""SellingPrice"":2.7,""VatRateId"":""A"",""DiscountLines"":[]},{""ProductGroupId"":""3"",""ProductGroupName"":""Frisdranken"",""ProductId"":""146"",""ProductName"":""Plat water"",""Quantity"":1,""QuantityUnit"":""P"",""SellingPrice"":2.6,""VatRateId"":""A"",""DiscountLines"":[]}]"
'Use JSON.Net to obtain a JArray
Dim jobj As JArray = JsonConvert.DeserializeObject(json)
'Extract just the fields you want from the data with a Linq projection into
'anonymous type with ProductName and Quantity fields
Dim ProductsAndQuantities = jobj.Select(Function(j)
Return New With {
.ProductName = j("ProductName"),
.Quantity = j("Quantity")}
End Function)
'build your DataTable
Dim dt As DataTable = New DataTable()
dt.Columns.Add("ProductName")
dt.Columns.Add("Quantity")
'Load the data table
For Each item In ProductsAndQuantities
Dim dr As DataRow = dt.NewRow()
dr("ProductName") = item.ProductName
dr("Quantity") = item.Quantity
dt.Rows.Add(dr)
Next
Related
New Help to retrieve original value from Json row Data the below code strip some "\"
-------------Json data-----------------
{
"type": "push",
"targets": ["stream"],
"push": {
"type": "mirror",
"source_device_iden": "ujzp6Xr9A4asjyjskXPzu8",
"source_user_iden": "ujzp6Xr9A4a",
"client_version": 354,
"dismissible": true,
"icon": "test",
"title": "ok",
"body": "Hi",
"application_name": "android",
"package_name": "com.android",
"notification_id": "1",
"notification_tag": "y9x5Q2YAI\/pqPhZwbaN6TpoW4eJhe0kAe0HfmWOQyWA=\n",
"conversation_iden": "{\"package_name\":\"com.android\",\"tag\":\"y9x5Q2YAI\\\/pqPhZwbaN6TpoW4eJhe0kAe0HfmWOQyWA=\\n\",\"id\":1}"
}
}
-------------------- VB code ---------------------------
Private Sub jsonData(JsonStr As String)
Dim json As String = JsonStr
Dim ser As JObject = JObject.Parse(json)
Dim data As List(Of JToken) = ser.Children().ToList
Dim Result as string
For Each item As JProperty In data
item.CreateReader()
Select Case item.Name
Case "push"
For Each msg As JObject In item
Result = msg("conversation_iden")
Next
End Select
Next
End Sub
--------------------------- resulet -----------------------------
Result = "{"package_name":"com.com.android","tag":"y9x5Q2YAI/pqPhZwbaN6TpoW4eJhe0kAe0HfmWOQyWA=\n","id":1}"
original value :
"{\"package_name\":\"com.android\",\"tag\":\"y9x5Q2YAI\/pqPhZwbaN6TpoW4eJhe0kAe0HfmWOQyWA=\n\",\"id\":1}"
I have a JSON response like this:
{ "cod": "OK",
"list": [
{ "date": "31\/10\/2018", "count": "109", "name": "PAUL" },
{ "date": "30\/09\/2018", "count": "103", "name": "LUKE" }
]}
I use:
Dim jss As New JavaScriptSerializer
Dim Response = jss.Deserialize(Of Object)(strResponse)
Dim Cod = Response("cod")
Then:
Dim Lista_documents = Response("list")
And I have an object with the list of documents.
How can I populate a new DataTable?
I am not aware of any methods that will do it automatically, so probably with a Loop.
Dim table As New DataTable
table.Columns.Add("date", GetType(Date))
table.Columns.Add("count", GetType(Integer))
table.Columns.Add("name", GetType(String))
For Each Li In Lista_documents
table.Rows.Add(Li("date"), Li("count"), Li("name"))
Next
Adjust depending on how your Lista_documents is structured
OK i know how to parse using newtonsoft.
but I don't know how can i get every value of key inside parsed string
this is the json encoded string
{"result":[{"orderid":"94","imei":"clipper"},{"orderid":"93","item":"shoes"},{"orderid":"92","item":"bag"},{"orderid":"91","item":"shirt"}]}
Dim xreadingJson = Newtonsoft.Json.Linq.JObject.Parse(htmlcode)
Dim resultorder As String = xreadingJson.Item("result").ToString
then the result order is
[
{
"orderid": "94",
"item": "clipper"
},
{
"orderid": "93",
"item": "shoes"
},
{
"orderid": "92",
"item": "shoes"
},
{
"orderid": "91",
"item": "bag"
}
]
On looping how can I get the value of orderid and item.
thank you
Update:
I resolved it using this code
Dim o As JObject = JObject.Parse(htmlcode)
Dim results As List(Of JToken) = o.Children().ToList
For Each item As JProperty In results
item.CreateReader()
'MsgBox(item.Value)
If item.Value.Type = JTokenType.Array Then
For Each subitem As JObject In item.Values
MsgBox(subitem("orderid"))
MsgBox(subitem("item"))
Next
End If
Next
I believe Newtonsoft's JObject has a JObject.GetValue("property_name") method
Hey I need help in deserializing this:
{
"success": true,
"rgInventory": {
"2722309060": {
"id": "2722309060",
"classid": "939801430",
"instanceid": "188530139",
"amount": "1",
"pos": 1
},
"2722173409": {
"id": "2722173409",
"classid": "937254203",
"instanceid": "188530139",
"amount": "1",
"pos": 2
},
"2721759518": {
"id": "2721759518",
"classid": "720293857",
"instanceid": "188530139",
"amount": "1",
"pos": 3
},
"2721748390": {
"id": "2721748390",
"classid": "310777652",
"instanceid": "480085569",
"amount": "1",
"pos": 4
}
}
}
at the end it should look like:
2722309060#2722173409#2721759518#2721748390
Dim result = JsonConvert.DeserializeObject(jsonstring) 'deserialize it
Dim tempfo As String = result("rgInventory").ToString 'get rgInventory
Console.WriteLine(tempfo)
how i can deserialize all 'id's?
The json contains a Dictionary of items, the IDs you want are the keys. If you deserialized it, you could get them from the Dictionary. Otherwise, you can use JParse and linq to get them:
Dim jstr As String = ...
' parse the json
Dim js As JObject = JObject.Parse(jstr)
' extract the inventory items
Dim ji As JObject = JObject.Parse(js("rgInventory").ToString)
' get and store the keys
Dim theIds As List(Of String) = ji.Properties.Select(Function(k) k.Name).ToList()
I suspect that when "success" is false that the resulting items list will be empty. Test that it works:
For Each s As String In theIds
Console.WriteLine(s)
Next
Result:
2722309060
2722173409
2721759518
2721748390
I am trying to take the value of a result JSON using VB(JSON.NET).
JSON
"paging": - {
"offset": 0,
"limit": 200,
"total": 6,
},
"results": - [
- {
"user_id": 010101,
"name": "Name X",
"nickname": "XXXX",
"points": 0
},
- {
"user_id": 0222101,
"name": "Name X2",
"nickname": "YYYY",
"points": 0
}
My code:
request = DirectCast(WebRequest.Create(campos.url), HttpWebRequest)
response = DirectCast(request.GetResponse(), HttpWebResponse)
reader = New StreamReader(response.GetResponseStream())
Dim rawresp, teste As String
rawresp = reader.ReadToEnd()
Dim jResults As JObject = JObject.Parse(rawresp)
'Dim results As List(Of JToken) = jResults.Children().ToList()
teste = jResults("results")("nickname").ToString()
I need to take the nickname, however do not achieve. In the truth, achieve only take values of the ítem PAGING and any of RESULTS.
But is giving error, he does not achieve to find the values.
Help me?
tks!