I have a standard JSON array returned to me which I have deserialized with JsonConvert, below being a simplified representative of my situation:
[
{
"name": "John",
"age": "21",
},
{
"name": "Sally",
"age": "18",
},
{
"name": "Harry",
"age": "25",
}
]
...
Public Class myExample
Public Property name as String
Public Property age as Integer
End Class
...
Dim serverResponse as string = reader.ReadToEnd()
Dim jsonResult = JsonConvert.DeserializeObject(Of List(Of myExample))(serverResponse)
I can easily retrieve the values of name or age given the item index, e.g.,
Dim someValue = jsonResult.Item(1).name ' returns Sally
Dim someOther = jsonResult.Item(1).age ' returns 18
Yet I wish to return the index (integer) of the array given the name: i.e., how might I search for Sally and retrieve the Item index integer (in this case 1), or in the case of John, return 0? I have not been able to achieve this with jsonResult.IndexOf(), or jsonResult.FindIndex() and searches have not been fruitful.
I have an extensive JSON and wish to loop through names on a dataGridViewer column, returning the ages of all names to another column.
Thanks for helping a novice out!
Since deserialization in this manner results in List(of T), the index to the above example can be found through, for example:
jsonResult.FindIndex((Function(s) s.name.Equals(someNameToCheck))))
or any other method of finding an item in a list.
Related
When attempting to serialize a DataTable to JSON using a custom format, I have some trouble adding JObjects (representing a row) to my JArray (representing the table).
So what I hope to make the table look like is something like this:
[
"Orderline" : {"Item": "Table", "Quantity": "5", "Amount": "50$"},
"Orderline" : {"Item": "Chair", "Quantity": "20", "Amount": "30$"},
"Orderline" : {"Item": "Couch", "Quantity": "2", "Amount": "500$"}
]
I have tried using the Add method to the JArray but I cannot seem to get it to work properly.
At the beginning I have set the JArray to new JArray, so that it is in memory and then I will go along and add the JObjects to it one at a time.
I can see that the add method takes two arguments (Item as JToken and content as object) which makes me a little confused because I do not see anyone else addressing both arguments in other code snippets I have seen online.
JArray is a instance of class Newtonsoft.Json.Linq.JArray.
Desired JObject output:
{
"Orderlines": {
"Item": "Table",
"Quantity": "5",
"Amount": "50"
}
}
Draft code:
Dim JsonObejct as JObject
Dim MyArray as Jarray
Dim Table as datatable
Set MyArray = new JArray
for each row in table
JsonObject = Jobject.FromObject(
New With {
Key.Orderlines = New With{
key.Item = row("Item").Tostring,
key.Quantity = row("Quantity").tostring,
key.Amount = row("Amount").tostring
}
}
)
Myarray.add(JsonObject)
Next row
I work in UiPath Studio and do not have the code in the same place its separated in multiple activities, so please don't get caught in wrongly defined details in the code, everything works until the add to jarray part.
I use Vb .net in UiPath Studio so I would appreciate a solution on how to add the JObects to my JArray in vb .net.
If you simply need to fix your compilation, the following compiles successfully and generates the required JSON structure:
Public Module DataTableJsonExtensions
Public Function ToSpecificOrderlines(ByVal table As DataTable) As JArray
Dim MyArray as JArray = New JArray
For Each row As DataRow In table.Rows
' Possibly you need to use CultureInfo.InvariantCulture in your ToString calls
Dim JsonObject As JObject = JObject.FromObject(
New With {
Key.Orderlines = New With{
key.Item = row("Item").ToString,
key.Quantity = row("Quantity").ToString,
key.Amount = row("Amount").ToString
}
}
)
MyArray.add(JsonObject)
Next row
Return MyArray
End Function
End Module
And then do
Dim myArray as JArray = DataTableJsonExtensions.ToSpecificOrderlines(table)
Demo fiddle #1 here.
However, it seems preferable to generalize the code to serialize any set of DataTable columns by name, with an optional specified format:
Public Module DataTableJsonExtensions
' Serialize all the columns of the table to an Orderlines array
Public Function ToOrderlines(ByVal table As DataTable) As JArray
Return ToOrderlines(table, table.Columns.Cast(Of DataColumn).Select(Function(c) c.ColumnName).ToArray())
End Function
' Serialize the selected columns of the table to an Orderlines array with the specified culture and formats (if any)
Public Function ToOrderlines(ByVal table As DataTable, ByVal columns() As String, Optional ByVal culture As CultureInfo = Nothing, Optional ByVal formats As IDictionary(Of String, String) = Nothing) As JArray
culture = If (IsNothing(culture), CultureInfo.InvariantCulture, culture)
Dim array as JArray = New JArray
For Each row As DataRow In table.Rows
Dim obj As JObject = new JObject (
new JProperty("Orderlines",
New JObject(columns.Select(Function(c) New JProperty(c, String.Format(culture, If(Not IsNothing(formats) AndAlso formats.ContainsKey(c), formats(c), "{0}"), row(c)))))
)
)
array.add(obj)
Next row
Return array
End Function
End Module
Then, to get the JSON shown at the top of your question, with the $ appended to the "Amount" property's value, do:
Dim formats = New Dictionary(Of String, String) From {{"Amount", "{0}$"}}
Dim myArray as JArray = DataTableJsonExtensions.ToOrderlines(table, {"Item", "Quantity", "Amount"}, Nothing, formats)
If you don't need the formatting after all, you call it as follows:
Dim myArray as JArray = DataTableJsonExtensions.ToOrderlines(table, {"Item", "Quantity", "Amount"})
Notes:
You can use DataTableJsonExtensions.ToOrderlines(table) to serialize all columns of the table to an Orderlines array.
In general serialization should always be performed using the invariant culture so that JSON serialized in one locale (say, the United States) can be deserialized in another locale (say, Europe) which uses a different decimal separator or DateTime format. ToString(), however, formats its values using the current culture. Thus I provided the option for you to specify a formatting culture in case you really want to serialize in CultureInfo.CurrentCulture. Passing Nothing for the culture results in using the invariant culture.
In the JSON shown in the top of your question you append a $ to the Amount column, so I provided an optional dictionary of formats in case you need to add that when serializing.
Demo fiddle #2 here.
I have a JSON like this:
[
{
"areas": [
{
"area": "New York",
"isDay": true,
"temp": "14"
},
{
"area": "Washington DC",
"isDay": true,
"temp": "30"
},
{
"area": "Los Angles",
"isDay": false,
"temp": "54"
},
{
"area": "San Diego",
"isDay": true,
"temp": "59"
}
],
"status": true,
"code": 200,
"created_at": "2019/06/18 22:26:34.475",
"request_time": "2019/06/18 22:25:28.306"
}
]
I'm trying to get the value of the the area value on the first/second/third object under the Areas array.
I've tried this code to get the value of the first area of the array, which in this case is "New York".
'Note that 'rawJSON' is a string variable that contains the JSON.
Dim jResults As JObject = JObject.Parse(rawJSON)
Dim naming As String = jResults("areas")("name")
However when I run this, I get and exception:
Newtonsoft.Json.JsonReaderException: 'Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.'
I am trying to write a function that imports all 'areas' in each array with their identifier of area (such as New York/Washington DC) into a ListBox (so the ListBox will contain, in order: New York, Washington DC, Los Angles).
How can I do this?
There are a couple of problems here:
Your JSON begins and ends with square brackets, so that means it is an array, not a single object. Since it is an array you need to use JArray.Parse() to parse it instead of JObject.Parse().
Inside your array, there is a single object which contains a property areas along with a few other properties like status and code. The value of the areas property is also an array, as denoted by square brackets. So your code needs to account for both of those arrays by adding the indexes. If you want the first item, that would be index zero.
There is no property called name anywhere in the JSON you posted. Instead, the names of the cities are each in a property called area.
Putting it all together, if you want to get the name New York from this JSON, you would do it like this:
Dim jResults As JArray = JArray.Parse(rawJSON)
Dim name As String = jResults(0)("areas")(0)("area")
Demo: https://dotnetfiddle.net/eAfEsx
If you're trying to get multiple names, then you would need a loop. You could do something like this:
Dim names As New List(Of String)()
For Each area As JObject In jResults(0)("areas")
Dim name As String = area("area")
names.Add(name)
Next
Demo: https://dotnetfiddle.net/BtSa6O
As you can see from the JSON structure you have posted, the JSON starts with a square bracket. That's the start of an array, the StartArray: when the JsonReader starts reading the JSON, the first TokenType is a JsonToken.StartArray (what the error is telling you).
Then there's an Object, defined by the braces: { }.
So you have an Array or List of Objects.
The first property of this object (areas) is of Type Array (or List).
It defines a collection of Objects.
Other properties, (status, code, etc.) have a single value.
[
{
"areas": [ { ... }, { ... } ]
"other": "other value"
}
]
You can parse the outer Array with JArray. The first element in the array (and the only one you're showing here), is the outer array. The first property of the first element in the outer array is the areas Array.
So you could write:
Dim areasArray = JArray.Parse(rawJson)(0)("areas")
Dim firstAreaValue = areasArray(0)("area").ToString()
The value of firstAreaValue is then "New York".
This is can be tiresome and prone to errors. You can generate a class Model from the JSON and use this model to deserialize the JSON to .Net classes, making it easier to handle, use as Source of Data, modify, serialize back etc.
Your JSON is an Array of Objects, so let's build the Root Object as a .Net class:
Note that I've set the Type of the CreatedAt and RequestTime properties to DateTime (Date): the string format of the DateTime, "2019/06/18 22:26:34.475", can be deserialized correctly.
DateTimeOffset is probably better. Your choice.
Public Class AreasRoot
<JsonProperty("areas")>
Public Property Areas As List(Of AreaObject)
<JsonProperty("status")>
Public Property Status As Boolean
<JsonProperty("code")>
Public Property Code As Long
<JsonProperty("created_at")>
Public Property CreatedAt As DateTime
<JsonProperty("request_time")>
Public Property RequestTime As DateTime
End Class
The Areas property is an Array or List of another type of object that contains 3 properties:
Public Class AreaObject
<JsonProperty("area")>
Public Property AreaArea As String
<JsonProperty("isDay")>
Public Property IsDay As Boolean
<JsonProperty("temp")>
Public Property Temp As Long
End Class
To deserialize using this Model, call JsonConvert.DeserializeObject(Of Type)(json).
The Type is, as mentioned, an Array or List(Of RootObject):
Dim areasArray = JsonConvert.DeserializeObject(Of List(Of AreasRoot))(rawJson).First()
Now New York is:
Dim firstArea = areasArray.Areas.First().Area
' Or
Dim firstArea = areasArray.Areas(0).Area
You can list all the Area names with:
For Each areaObj In areasArray.Areas
Console.WriteLine(areaObj.Area)
Next
Im using newtonsoft, and I need to get two specific fields on a Json response:
{
"id": "1233323",
"quoteOptions": [{
"copay": "20%",
"deductables": [{
"deductable": "$250",
"quoteOptions": [{
"pricingAndLimits": {
"reimbursement": "80%",
"copay": "20%",
"deductable": "$250",
"limit": "$4,000",
"annualPremium": "$413.00",
"monthlyPremium": "$34.00"
}
}]
}]
}]
}
I need to do this on a legacy intranet (VB.NET). I specifically need to get the monthlyPremium field and the id field.
I tried
Dim json As String = Result
Dim ser As JObject = JObject.Parse(json)
Dim data As List(Of JToken) = ser.Children().ToList
Dim output As String = ""
Dim Premium As String = ser("quoteOptions")("deductables")("quoteOptions")("pricingAndLimits")("monthlyPremium")
But I get an error at "deductables" (doesn't exist).
Thanks :)
I've made it work like this:
Money = ser("quoteOptions")(0)("deductables")(0)("quoteOptions")(0)("pricingAndLimits")("monthlyPremium")
Thanks CrlueD
My question is about a JSON file, of which I'm getting errors when putting certain values in a text component.
{
"response": {
"game_count": 4,
"games": [
{
"appid": 10,
"playtime_forever": 0
},
{
"appid": 20,
"playtime_forever": 0
},
{
"appid": 30,
"playtime_forever": 0
},
{
"appid": 40,
"playtime_forever": 0
}
]
}
}
I'm trying to get the values "10,20,30,40" from "appid" in a RichTextBox, one below the other. For this, I am using this code:
JsonString = file
Dim jsonObject As JObject = JObject.Parse(JsonString)
Dim JsonArray As JArray = JArray.Parse(jsonObject.SelectToken("games").ToString)
For Each item As JObject In JsonArray
RichTextBox2.Text = item.SelectToken("appid").ToString
Next
But I'm getting the following error in line 4 of the above code:
Object reference not set to an instance of an object
Is there a way to fix this? I believe my code is right. I'm using the NewtonSoft library.
You can use SelectTokens("..appid") to recursively descent the JSON token hierarchy and find values of properties named "appid", where ".." is the JSONPath recursive descent operator:
Dim jsonObject As JToken = JToken.Parse(JsonString)
Dim appids = jsonObject _
.SelectTokens("..appid") _
.Select(Function(t) CType(t, Long)) _
.ToList()
After finding all "appid" values I cast then to Long using an explicit conversion cast. You could cast to Int If you are certain that all appid values will be less than Int.MaxValue. Or you could cast then to String if you're not interested in the numeric values.
Working .Net fiddle.
I've been racking my brain for the past few hours.
I found that you can serialize the data and as such save the hassle of creating endlessly long .split commands for each of the seperate JSON values. So I've followed what a lot of people on SO and all over the web have done, by creating a JsonHelper class to Serialize and Deserialize the JSON that is passed from an ASP.net page.
This is my class:
Public Class NewTaskOb
Public Property Department As Integer
Public Property Type As Integer
Public Property AssignedTo As Integer
Public Property Priority As Integer
Public Property Title As String
Public Property JobCode As Integer
Public Property ParentTask As String
Public Property DueDate As Date
Public Property Scope As String
Public Property Comment As String
Public Property BudgetString As String
End Class
Then I have this:
<WebMethod()> Public Shared Function NewTaskRequest(task As String) As String
Dim newTask As NewTaskOb = DeserializeJSON.JsonDeserialize(Of NewTaskOb)(task)
My JSON is valid according to jsonlint, but when I pass the values through, I check by putting a breaker on the Dim newTask line, and when I hover my mouse over it, it says each of the values is empty (Department = 0, Type = 0, etc.) when, on the form, I am submitting values.
I have tried by using DataContractJsonSerializer, JsonHelper, and JavaScriptSerializer. Please note, I do not want to import another reference (i.e Newtonsoft.Json, or anything similar). I want to get this done using the aforementioned functions.
Thanks for your help in advance.
Edit (adding in the JSON string itself):
{
"NewTask": {
"Department": "-1",
"Type": "0",
"AssignedTo": "0",
"Priority": "-1",
"Title": "",
"JobCode": "-",
"ParentTask": "-",
"DueDate": "",
"Scope": "",
"Comment": "",
"BudgetString": ""
}
}
Edit:
While messing around with the code, I went back to the JavaScriptSerializer and found if I put Dim data = jss.Deserialize(Of Object)(task) in place of Dim data = jss.Deserialize(Of NewTaskOb)(task), then it shows up while stepping through that there is a dictionary in the data, that has the fields with the data.
Dictionary showing JSON correctly http://imageshack.com/a/img401/1174/a9ju.png
Any idea how I can extract these into the class variables?
Thanks in advance.
So I was testing your program on my system and noticed that it has Dictionary"2. Imagining what it could possibly mean, I had the idea that 2 could mean there is a dictionary nested within a dictionary - which could explain why you couldn't retrieve the values. So I edited away, and ended up with this line - Dim taskData = jss.Deserialize(Of Dictionary(Of Object, Object))(task). Now, when I run it I have put in Dim taskValues = taskData.Values, and I can reference each of the values using their respective index positions...
i.e taskValues(0) = Department, taskValues(1) = Type, etc.
Also, I removed the line where you were inserting the data into a Data Transfer Object, thus eliminating the "NewTask": nesting within the JSON, resulting in this JSON:
{
"Department": "-1",
"Type": "0",
"AssignedTo": "0",
"Priority": "-1",
"Title": "",
"JobCode": "-",
"ParentTask": "-",
"DueDate": "",
"Scope": "",
"Comment": "",
"BudgetString": ""
}