|
Hi Robert,
Great solution but I have a problem because I need ALL the results in an array and not just the first result.
Here is my json
{"normalizedSource":"magnificent","displaySource":"magnificent","translations":[{"normalizedTarget":"prachtige","displayTarget":"prachtige","posTag":"ADJ","confidence" .286,"prefixWord":"","backTranslations":[{"normalizedText":"beautiful","displayText":"beautiful","numExamples":15,"frequencyCount":3043},{"normalizedText":"wonderful","displayText":"wonderful","numExamples":15,"frequencyCount":1738},{"normalizedText":"magnificent","displayText":"magnificent","numExamples":15,"frequencyCount":539},{"normalizedText":"stunning","displayText":"stunning","numExamples":15,"frequencyCount":344},{"normalizedText":"gorgeous","displayText":"gorgeous","numExamples":15,"frequencyCount":226},{"normalizedText":"lovely","displayText":"lovely","numExamples":15,"frequencyCount":223},{"normalizedText":"splendid","displayText":"splendid","numExamples":15,"frequencyCount":221}]},{"normalizedTarget":"magnifieke","displayTarget":"magnifieke","posTag":"ADJ","confidence" .2587,"prefixWord":"","backTranslations":[{"normalizedText":"magnificent","displayText":"magnificent","numExamples":15,"frequencyCount":57}]},{"normalizedTarget":"schitterende","displayTarget":"schitterende","posTag":"ADJ","confidence" .1639,"prefixWord":"","backTranslations":[{"normalizedText":"beautiful","displayText":"beautiful","numExamples":15,"frequencyCount":186},{"normalizedText":"brilliant","displayText":"brilliant","numExamples":15,"frequencyCount":91},{"normalizedText":"magnificent","displayText":"magnificent","numExamples":15,"frequencyCount":91},{"normalizedText":"stunning","displayText":"stunning","numExamples":15,"frequencyCount":78},{"normalizedText":"splendid","displayText":"splendid","numExamples":15,"frequencyCount":70},{"normalizedText":"gorgeous","displayText":"gorgeous","numExamples":15,"frequencyCount":57},{"normalizedText":"dazzling","displayText":"dazzling","numExamples":5,"frequencyCount":37}]},{"normalizedTarget":"geweldig","displayTarget":"geweldig","posTag":"ADJ","confidence" .0938,"prefixWord":"","backTranslations":[{"normalizedText":"great","displayText":"great","numExamples":15,"frequencyCount":3285},{"normalizedText":"amazing","displayText":"amazing","numExamples":15,"frequencyCount":609},{"normalizedText":"awesome","displayText":"awesome","numExamples":15,"frequencyCount":422},{"normalizedText":"wonderful","displayText":"wonderful","numExamples":15,"frequencyCount":271},{"normalizedText":"terrific","displayText":"terrific","numExamples":5,"frequencyCount":99},{"normalizedText":"magnificent","displayText":"magnificent","numExamples":8,"frequencyCount":68},{"normalizedText":"fabulous","displayText":"fabulous","numExamples":15,"frequencyCount":30}]},{"normalizedTarget":"grandioos","displayTarget":"grandioos","posTag":"ADJ","confidence" .0929,"prefixWord":"","backTranslations":[{"normalizedText":"magnificent","displayText":"magnificent","numExamples":3,"frequencyCount":9},{"normalizedText":"grandiose","displayText":"grandiose","numExamples":3,"frequencyCount":6}]},{"normalizedTarget":"fantastische","displayTarget":"fantastische","posTag":"ADJ","confidence" .0567,"prefixWord":"","backTranslations":[{"normalizedText":"fantastic","displayText":"fantastic","numExamples":15,"frequencyCount":1200},{"normalizedText":"great","displayText":"great","numExamples":15,"frequencyCount":498},{"normalizedText":"amazing","displayText":"amazing","numExamples":15,"frequencyCount":254},{"normalizedText":"fabulous","displayText":"fabulous","numExamples":15,"frequencyCount":126},{"normalizedText":"wonderful","displayText":"wonderful","numExamples":15,"frequencyCount":78},{"normalizedText":"superb","displayText":"superb","numExamples":15,"frequencyCount":61},{"normalizedText":"magnificent","displayText":"magnificent","numExamples":11,"frequencyCount":31},{"normalizedText":"terrific","displayText":"terrific","numExamples":5,"frequencyCount":15}]},{"normalizedTarget":"grootse","displayTarget":"grootse","posTag":"ADJ","confidence" .0479,"prefixWord":"","backTranslations":[{"normalizedText":"great","displayText":"great","numExamples":15,"frequencyCount":69},{"normalizedText":"grand","displayText":"grand","numExamples":15,"frequencyCount":62},{"normalizedText":"magnificent","displayText":"magnificent","numExamples":3,"frequencyCount":9}]}]}
Dim _value As String = JsonHelper.GetJson(jsonResponse, "displayText")
In "backTranslations" I need ALL the "displayText"
Now I only get the First value
Could you please advisse how I should do that?
Many Thanks
Ton
|
|
|
|
|
Robert, This has been a great help. Gave it a 5. It is great for grabbing a value. A lot of my json returns are nested and each child has several fields with the same name, eg "name". So I modified your code to get the iteration to find the child node and then go for the field value.
Imports System.IO
Imports Newtonsoft.Json.Linq
Public Class JsonHelper
Private Shared output As String
Public Shared Function GetJson(jData As String, fieldName As String, Optional Top As Integer = 0, Optional ChildNode As String = "") As String
Dim ser As JObject = JObject.Parse(jData)
Dim data() As JToken
Dim jT() As JToken
If Top = 0 Then
data = ser.Children().ToArray()
jT = data(0).First.ToArray()
Else
jT = ser.Children().ToArray()
End If
For Each itm As JProperty In jT
If ChildNode <> "" Then
If itm.Name = ChildNode Then
If itm.Value.Type = JTokenType.Array Then
Dim str As String = itm.First.First.ToString
GetJson(str, fieldName, 1)
End If
If itm.Value.Type = JTokenType.Object Then
Dim str As String = itm.First.ToString
GetJson(str, fieldName, 1)
End If
End If
Else
If itm.Name = fieldName Then
output = itm.Value.ToString
End If
If itm.Value.Type = JTokenType.Array Then
Dim str As String = itm.First.First.ToString
GetJson(str, fieldName, 1)
End If
If itm.Value.Type = JTokenType.Object Then
Dim str As String = itm.First.ToString
GetJson(str, fieldName, 1)
End If
End If
Next
Return output
End Function
End Class
I did change some of the parameter names.
You could edit it for deeper nesting as well.
I call JsonHelper after parsing the fieldname I am looking for using the standard json format.
eg - item.name, item.id
'txtField.text contains the field name I'm after
Dim sChild As String = String.Empty
Dim sField As String = String.Empty
If InStr(txtField.Text, ".") Then
sField = Microsoft.VisualBasic.Right(txtField.Text, txtField.Text.IndexOf("."))
sChild = Microsoft.VisualBasic.Left(txtField.Text, txtField.Text.IndexOf("."))
End If
txtResults.Text = JsonHelper.GetJson(sResults, sField, 0, sChild)
|
|
|
|