none
How to split multiple Json Strings RRS feed

  • Question

  • Hello- I need to split the json string so that I can convert them to datasets. Below is my Json any advis how to split them?

    {
    	""TC"":[{
    		""test_case"":""ITCFScenario12"",
    		""data_type"":""testcomplete"",
    	}]
    	""3"":[{
    	""scenariodescription"":""Create Client"",
    	}],
    	""7"":[{
    	""scenariodescription"":""CreateServicePlan"",
    	}],
    } 
    \n
    {
    	""TC"":[{
    		""test_case"":""ITCFScenario13"",
    		""data_type"":""testcomplete"",
    	}]
    	""3"":[{
    	""scenariodescription"":""Create Client"",
    	}],
    	""7"":[{
    	""scenariodescription"":""CreateServicePlan"",
    	}],
    },

    Sunday, March 3, 2019 12:33 PM

Answers

  • Hi mIN-MADIN,

    Thank you for posting here.

    For your question, if you want to convert json data to datasets, please refer to the code below with correct format of json data.

    Use Newtonsoft.Json. you could install from NuGet.

     string json = @"{
    	 'Table1': [
        {
          'id': 0,
          'item': 'item 0'
        },
        {
          'id': 1,
          'item': 'item 1'
        }
      ]
                               }";
    
                DataSet dataSet = JsonConvert.DeserializeObject<DataSet>(json);
    
                System.Data.DataTable dataTable = dataSet.Tables["Table1"];
    
                Console.WriteLine(dataTable.Rows.Count);
                // 2
    
                foreach (DataRow row in dataTable.Rows)
                {
                    Console.WriteLine(row["id"] + " - " + row["item"]);
                }

    Best Regards,

    Wendy


    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.

    Monday, March 4, 2019 4:23 AM
    Moderator

All replies

  • Hi mIN-MADIN,

    Could you please provide the JSON message that has correct opening and closing?

    Also could you please provide more information about you saying converting them to dataset. Please provide a sample format how you want to show the JSON message in there.


    &amp;lt;p&amp;gt;Thanks,&amp;lt;br/&amp;gt; Sabah Shariq &amp;lt;/p&amp;gt; &amp;lt;p style=&amp;quot;font-size:small&amp;quot;&amp;gt;[If a post helps to resolve your issue, please click the &amp;lt;font color=&amp;quot;blue&amp;quot;&amp;gt;&amp;amp;quot;Mark as Answer&amp;amp;quot; &amp;lt;/font&amp;gt;of that post or click &amp;lt;img alt=&amp;quot;Answered&amp;quot; src=&amp;quot;http://social.technet.microsoft.com/Forums/getfile/179394&amp;quot; title=&amp;quot;Answered&amp;quot;&amp;gt; &amp;lt;font color=&amp;quot;green&amp;quot;&amp;gt;&amp;amp;quot;Vote as helpful&amp;amp;quot; &amp;lt;/font&amp;gt;button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ] &amp;lt;/p&amp;gt;

    Sunday, March 3, 2019 3:14 PM
    Moderator
  • The format is not correct.

    You must be able to paste it with VS and [Paste Special][Paste JSON as Classes]

    Then, you can use DataContractJsonSerializer

    For example, for the first part, this can be pasted as classes :

    {
    	"TC":[{
    		"test_case":"ITCFScenario12",
    		"data_type":"testcomplete",
    	}],
    	"3":[{
    	"scenariodescription":"Create Client",
    	}],
    	"7":[{
    	"scenariodescription":"CreateServicePlan",
    	}],
    } 

    Sunday, March 3, 2019 4:56 PM
  • Hi mIN-MADIN,

    Thank you for posting here.

    For your question, if you want to convert json data to datasets, please refer to the code below with correct format of json data.

    Use Newtonsoft.Json. you could install from NuGet.

     string json = @"{
    	 'Table1': [
        {
          'id': 0,
          'item': 'item 0'
        },
        {
          'id': 1,
          'item': 'item 1'
        }
      ]
                               }";
    
                DataSet dataSet = JsonConvert.DeserializeObject<DataSet>(json);
    
                System.Data.DataTable dataTable = dataSet.Tables["Table1"];
    
                Console.WriteLine(dataTable.Rows.Count);
                // 2
    
                foreach (DataRow row in dataTable.Rows)
                {
                    Console.WriteLine(row["id"] + " - " + row["item"]);
                }

    Best Regards,

    Wendy


    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.

    Monday, March 4, 2019 4:23 AM
    Moderator