-1

Here is my situation:

I have a form that is collecting a list of items in a textarea as a JSON Object.

The form textarea looks like this:

<textarea id="listItems">
    [
     {"id":"1","name":"apple"},
     {"id":"2","name":"orange"},
     {"id":"3","name":"banana"}
    ]
</textarea>

I need to be able to parse that string and POST each item into a SQL Table.

ItemID | ItemName
-----------------
1      | apple
2      | orange
3      | banana

I don't think I have a good understanding of using the JavaScriptSerializer Class. I'm using VB.net

I don't have any server-side code yet but I know that I have to parse out the JSON string and then loop through it and then save each item.

Couldn't I just convert the JSON string into a DataTable and then loop through that temporary table?

Not sure. I'm trying to figure this out but some help would be useful.

Also I'm referencing a few SO posts to see if maybe I can figure this out

This one

This one

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
cpt-crunchy
  • 391
  • 4
  • 23
  • It would be easier to deserialize the JSON into a collection of objects, and then loop through the collection, inserting each instance into your table. No need to go to the extra work of creating a `DataTable` in memory. – Tim Mar 02 '18 at 07:09

2 Answers2

0

So expecting the following Json Object at the Server

[
  {"id":"1","name":"apple"},
  {"id":"2","name":"orange"},
  {"id":"3","name":"banana"}
]

you would be able to do this:

Public Function DoSomething()

   Dim jsonObject = "[{""id"":""1"",""name"":""apple""},{""id"":""2"",""name"":""orange""},{""id"":""3"",""name"":""banana""}]"
   Dim obj = New JavaScriptSerializer().Deserialize(Of TestObject())(jsonObject)

End Function

With this Model

Public Class TestObject
    Public Property id As Integer
    Public Property name As String
End Class

At runtime you will have an array of Objects in obj which you can then manipulate and fill into a DataTable or directly feed to the Database. Converting the Json into an Object is not the only option you have here but it is a convenient one.

Since you did not specify the context you are working in it is a little unclear what might be the best solution to your case. Maybe you want to do a little research on different Json Frameworks (for example Json.net) as well as Data-Handling with Databases so you get ideas about the how and when.

Severin Jaeschke
  • 661
  • 7
  • 22
  • Perhaps this is my confusion but can I swap the hard-coded jsonObject with Dim jsonObject = listItems.Text since I have a form submitting the JSON object inside the text area. – cpt-crunchy Mar 02 '18 at 16:11
  • Yes. The hard coded object is just there for clarification. You can exchange it with any string that contains JSON. Whether you are getting that from a control or somewhere else is not important. – Severin Jaeschke Mar 02 '18 at 16:43
  • I tried this and I got an error: System.NullReferenceException: Object reference not set to an instance of an object. Also to provide a bit more of context I am using a Generic Entity to Set the Value in the Database. I am doing as such to set the value: Wth GenericEntity.Add().SetValue("id", obj(0)) End With – cpt-crunchy Mar 02 '18 at 20:35
  • I am unfamiliar with generic entities but your NullReferenceException suggests that you are referencing something that does not exist / has not been initialized yet. You need to verify which variable is null and then you may be able to discern what exactly your problem is. But without the actual error information and the code you are using it's hard to do a remote diagnosis. Maybe start with verifying that your data has been entered. Then check if the objects are in the array and at last you'd need to check if your entity provides all the properties you are referencing – Severin Jaeschke Mar 05 '18 at 08:02
  • So I checked my form using httpbin.org and it is posting data. However, it is posting the id as the value and auto-increment the id. So the output looks as such. { 0: 11, 1:12, 2:13 }. Honestly I'm only interested in posting the IDs or values in the example i provided in my comment. I'm unsure as to the format of the output I gave you this comment. I'm expecting, as well as my server side code, to receive an array of Objects. Perhaps this is what is causing the Null-reference? – cpt-crunchy Mar 05 '18 at 15:30
  • Well if you have the above Json Obejct at the Server it can't be parsed into the Object because it doesn't have the expected structure. To help you further I would need to see your code. As is all I can do is guess. – Severin Jaeschke Mar 06 '18 at 07:26
  • I was able to find a solution for my problem. I'll post my answer and the link to the question that answered – cpt-crunchy Mar 06 '18 at 23:17
0

This was the best answer I found and I adjusted it to meet my needs.

I've also included the link to the duplicate POST that answered my question.

I'm also referencing another similar post which has a similar situation.

This is the link to the .Net Fiddle that demonstrates what I was looking for.

DEMO

How to deserialize JSON which can be an array or a single object

How to handle both a single item and an array for the same property using JSON.net

cpt-crunchy
  • 391
  • 4
  • 23