Click here to Skip to main content
14,486,715 members
Rate this:
Please Sign up or sign in to vote.
See more:
Imports Newtonsoft.Json
 
 Public Class root
        Public Property book As item()
    End Class

    Public Class item
        Public Property Name As String
        Public Property Expiry As DateTime
        Public Property Price As Double
        Public Property Sizes As String()
    End Class

The existing JSON,
{"book":[{"Name":"tea","Expiry":"2020-01-01T00:00:00","Price":80.0,"Sizes":null}]}

I want to add another item to above Json string using VB2010.
The result must be,
{"book":[{"Name":"tea","Expiry":"2020-01-01T00:00:00","Price":80.0,"Sizes":null}{"Name":"milk","Expiry":"2020-04-01T00:00:00","Price":180.0,"Sizes":null}]}

I have no idea to add new data to an existing JSON..Help me..

What I have tried:

Imports Newtonsoft.Json

 Public Class root
        Public Property book As item()
    End Class

    Public Class item
        Public Property Name As String
        Public Property Expiry As DateTime
        Public Property Price As Double
        Public Property Sizes As String()
    End Class
Posted
Updated 21-Mar-20 19:47pm
v2
Comments
Richard Deeming 28-Jan-20 14:24pm
   
NB: If you're going to be adding items, you'll probably find it easier to declare the property as a list instead of an array. Both will work with your JSON.
Public Property book As List(Of item)
Rate this:
Please Sign up or sign in to vote.

Solution 1

You don't, you create a new item instance , fill in it's details, add it to your instance of root, and generate a new JSON from that. When you create the JSON data, it includes all changes made to the data your VB code is holding.

JSON should not be edited directly: it is not a database - it's a data transfer format and should be created just before to send it or save it.
   
Comments
Member 13358124 28-Jan-20 3:10am
   
Thank you!
Rate this:
Please Sign up or sign in to vote.

Solution 2

Please follow the below code. I tested for a project I am doing in VS2019 15.9.2 VB.net

My Json Data :
{
  "Logs": [
    {
      "Time": "Mar 22 2020  4:17AM",
      "Log": "New Alarm Logged"
    },
    {
      "Time": "Mar 23 2020  7:37AM",
      "Log": "Deleted automatically because Alarm is cleared."
    }
  ]
}

We need to add Activity Logs with time stamps ( That's what I am trying to do ), which is pretty similar with your application.
Class declaration as shown below
Public Class Log
Public Property Time As String
Public Property Log As String
End Class

Public Class ActivityLog
Public Property Logs As List(Of Log) 'Note the usage of List(of ..)
End Class

Now we will make a function to collect the data to be entered into the Log array of json object.
Public Function _json_Fn_LogAppend(ByVal InJson As String, ByVal InValue As String) As String
_json_Fn_LogAppend = "" 'Pass a blank string to avoid null errors.
Try
Dim _ActivityLog As ActivityLog = New ActivityLog 
Dim _Log As Log = New Log
_Log.Time = Now().ToLongDateString
_Log.Log = InValue
_ActivityLog = JsonConvert.DeserializeObject(Of ActivityLog)(InJson) 'Deserialize existing Data
_ActivityLog.Logs.Add(_Log) 'Add the new data to array. Make sure logs (array in json ) to be declared as List (of..)
_json_Fn_LogAppend = JsonConvert.SerializeObject(_ActivityLog, Formatting.Indented) 'Serialize the Json again.
Console.WriteLine(_json_Fn_LogAppend)
Return _json_Fn_LogAppend
Catch ex As Exception
Console.WriteLine("ERROR/EX  From Function <_json_Fn_LogAppend> : " + ex.ToString)
End Try
Return _json_Fn_LogAppend
End Function

Usage of Function where
Dim JsonDataCompiled As String = _json_Fn_LogAppend(Json, "Deleted automatically because Alarm is cleared.")

Json in the function is the existing json Data.
   
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100