none
JSON READER AND CREATOR RRS feed

  • Question

  • Hi,

    How can I store data from data table to a variable where data will generate something like a JSON file format. Once data has been generated to that variable, how can i use it to retrieve a specific information. I did some research about it. And I found lots of sample but it just got me confused coz it's not that exactly what I'm looking for. So I haved decided to ask for your help to learn about JSON.

    Tuesday, May 22, 2018 7:12 AM

All replies

  • You can add "Newtonsoft JSON" from the NuGet package manager; it is one of the most popular solutions.

    Or you can add a project reference to System.Runtime.Serialization and then use DataContracts and serialization:
    https://docs.microsoft.com/en-us/dotnet/api/system.runtime.serialization.json.datacontractjsonserializer?view=netframework-4.7.2

    Or you can just use a System.Text.StringBuilder and generate your own JSON string by looping over the contents of the data table.  JSON is pretty simple to generate.

    http://json.org/


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, May 22, 2018 11:47 AM
    Moderator
  • Here is a "How-To" on using the framework supplied JSON serialization:

    https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-serialize-and-deserialize-json-data


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, May 22, 2018 11:48 AM
    Moderator
  • I prefer to explore and have a sample code than to get confused by reading tons of documentations. Hope you understand.
    Tuesday, May 22, 2018 11:50 PM
  • Hi,

    How can I store data from data table to a variable where data will generate something like a JSON file format. Once data has been generated to that variable, how can i use it to retrieve a specific information. I did some research about it. And I found lots of sample but it just got me confused coz it's not that exactly what I'm looking for. So I haved decided to ask for your help to learn about JSON.

    Hi zir0hn,

    According to your description, you want to store data from datatable into json string, am I right? If yes, you can take a look the following code, this article is about convert datatable to json string.

    https://www.aspdotnet-suresh.com/2013/05/c-convert-datatable-to-json-string-in-c.html

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 23, 2018 2:46 AM
    Moderator
  • Hi,

    How can I store data from data table to a variable where data will generate something like a JSON file format. Once data has been generated to that variable, how can i use it to retrieve a specific information. I did some research about it. And I found lots of sample but it just got me confused coz it's not that exactly what I'm looking for. So I haved decided to ask for your help to learn about JSON.

    Hi zir0hn,

    According to your description, you want to store data from datatable into json string, am I right? If yes, you can take a look the following code, this article is about convert datatable to json string.

    https://www.aspdotnet-suresh.com/2013/05/c-convert-datatable-to-json-string-in-c.html

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    When I'm about to read the the string (generated into json format) I have an error. I don't know if its with the string value or in my class.

    Wednesday, May 23, 2018 3:39 AM
  • I prefer to explore and have a sample code than to get confused by reading tons of documentations. Hope you understand.

    So you didn't even click the links?  I have given you paths of "exploration", not "tons of documentation".

    The first link is to a popular installation package that will parse/create JSON for you.

    The second link is for documentation, however, I followed up with another post that contains a step-by-step walkthrough.

    The third link in my first reply is the JSON documentation itself... however, JSON is so simple that the documentation is literally one short page.

    You did not provide any examples of what your data looks like in the data table, or what you want the output to look like, so I'm not sure how you expect us to provide any kind of detailed example that would apply to your scenario.

    And then, to top it all off, you tried to use an example from 2013 which is both outdated and only partially relevant.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, May 23, 2018 11:13 AM
    Moderator
  • I prefer to explore and have a sample code than to get confused by reading tons of documentations. Hope you understand.

    So you didn't even click the links?  I have given you paths of "exploration", not "tons of documentation".

    The first link is to a popular installation package that will parse/create JSON for you.

    The second link is for documentation, however, I followed up with another post that contains a step-by-step walkthrough.

    The third link in my first reply is the JSON documentation itself... however, JSON is so simple that the documentation is literally one short page.

    You did not provide any examples of what your data looks like in the data table, or what you want the output to look like, so I'm not sure how you expect us to provide any kind of detailed example that would apply to your scenario.

    And then, to top it all off, you tried to use an example from 2013 which is both outdated and only partially relevant.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    First, I have already installed, and reference the json on my project before I asked question here. I clicked and tried to read the links you provided but still can't get it. But thanks to Cherry Bu, coz she provided me a link with a sample code that's why I had made a class for converting datatable values into json string. And the only problem is that, i can't get the value I want (from variable that contains generated in json format). I easily get it by tracing in a sample code not with some documentation, theory, definitions, library, etc. Maybe you are an expert that's why you prefer to read and read documentations and solve the issues. But not with me, Im a newbie compare to you that's why you didn't help me by providing that links sorry Reed Kimble, but still thank you.
    Thursday, May 24, 2018 2:17 AM
  • Hi,

    How can I store data from data table to a variable where data will generate something like a JSON file format. Once data has been generated to that variable, how can i use it to retrieve a specific information. I did some research about it. And I found lots of sample but it just got me confused coz it's not that exactly what I'm looking for. So I haved decided to ask for your help to learn about JSON.

    Hi zir0hn,

    According to your description, you want to store data from datatable into json string, am I right? If yes, you can take a look the following code, this article is about convert datatable to json string.

    https://www.aspdotnet-suresh.com/2013/05/c-convert-datatable-to-json-string-in-c.html

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    When I'm about to read the the string (generated into json format) I have an error. I don't know if its with the string value or in my class.

    Well from the link provided by Cherry Bu the use a New Dictionary(Of String, Object). I don't see in your code which doesn't show much code where you create a New Dictionary. Therefore the Dictionary would not exist as Dim Dict As Dictionary(Of String, String). It needs to be instantiated first.

    La vida loca

    Thursday, May 24, 2018 4:17 AM
  • You still have not provided an example of your data, or explained how you want the output JSON to look.  Nor have you explained what part is confusing to you.  You say you "installed and reference the json", but what did you install?  How can we help you further if we do not know what you have done already and we don't know what you don't understand?

    Here is a quick example of looping a datatable, generating a JSON string, parsing that string using Serialization.Json, and then reading some values:

    The test dataset:

    The code:

    'load an example dataset datatable with data
    Dim dataSet As New DataSet1
    For i = 1 To 10
        dataSet.DataTable1.Rows.Add(i, "Number " & i.ToString, i * 5)
    Next
    
    'build JSON string 
    Dim sb As New Text.StringBuilder
    'open JSON object
    sb.AppendLine("{")
    For i = 0 To dataSet.DataTable1.Rows.Count - 1
        Dim dr = dataSet.DataTable1.Rows(i)
        'create row element
        sb.AppendFormat("   ""Row{0}"" : ", i)
        sb.Append("{")
        sb.AppendLine()
        'add column elements
        For j = 0 To dataSet.DataTable1.Columns.Count - 1
            Dim dc = dr.Table.Columns(j)
            If dc.DataType Is GetType(Boolean) OrElse IsNumeric(dr.Item(j)) Then
                'if boolean or number, no quotes needed around value
                sb.AppendFormat("      {0}{1}{0} : {2}", ControlChars.Quote, dc.Caption, dr.Item(j))
            Else
                'otherwise, put quotes around value
                sb.AppendFormat("      {0}{1}{0} : {0}{2}{0}", ControlChars.Quote, dc.Caption, dr.Item(j))
            End If
            If j < dataSet.DataTable1.Columns.Count - 1 Then sb.Append(", ")
            sb.AppendLine()
        Next
        'close row element
        sb.AppendLine("      }")
    Next
    'close table object
    sb.AppendLine("}")
    'show JSON
    MessageBox.Show(sb.ToString)
    
    'Parse JSON
    Dim bytes() = System.Text.Encoding.ASCII.GetBytes(sb.ToString)
    sb.Length = 0
    Dim json = Runtime.Serialization.Json.JsonReaderWriterFactory.CreateJsonReader(bytes, Xml.XmlDictionaryReaderQuotas.Max)
    'read parsed JSON, just a rough example to see results,
    '  your actual reading depends on what you are trying to do
    Do While json.Read()
        Dim s = json.ReadContentAsString
        If s.Length > 0 Then sb.AppendLine(s)
    Loop
    'show the JSON was read
    MessageBox.Show(sb.ToString)
    If you are using VS2017, don't use that old web javascript interface that Cherry posted.  Use the current serialization provider.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Thursday, May 24, 2018 11:59 AM
    Moderator