Using the DLL from here
http://james.newtonking.com/json/help/index.html
1. JSON to DataTable
If you are having same number level in JSON string like this
[{
"href": "http://www.merchantgould.com/#"
,"text": "Name"
}
,
{
"href": "http://www.merchantgould.com/Profile_Arvind_Kamal.aspx"
,"text": "Kamal K. Arvind"
}]
Then You can have the JSON data in DataTable.
Here i have stored the JSON string in TextFile.
Namespaces
C#
using System.IO;
using System.Data;
using Newtonsoft.Json;
VB.Net
Imports System.IO
Imports System.Data
Imports Newtonsoft.Json
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
StreamReader stRead = new StreamReader(Server.MapPath("JSon.txt"));
DataTable table = JsonConvert.DeserializeObject<DataTable>(stRead.ReadToEnd());
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim stRead As StreamReader = New StreamReader(Server.MapPath("JSon.txt"))
Dim table As DataTable = JsonConvert.DeserializeObject(Of DataTable)(stRead.ReadToEnd())
End Sub
Screenshot
2. JSON to Class object
If you are having complex Json then you need to create the class like this
{
"name": "Merchant_Gould ",
"count": 141,
"frequency": "realtime",
"version": 2,
"newdata": true,
"lastrunstatus": "success",
"lastsuccess": "Tue May 13 2014 15:02:14 GMT+0000 (UTC)",
"results": {
"collection1": [
{
"Name": {
"href": "http://www.merchantgould.com/#",
"text": "Name"
},
"Level": "",
"Location": ""
},
{
"Name": {
"href": "http://www.merchantgould.com/Profile_Arvind_Kamal.aspx",
"text": "Kamal K. Arvind"
},
"Level": "Technical Advisor",
"Location": "Atlanta"
}
]
}
}
The above JSON is stored in TextFile.
Namespaces
C#
using System.IO;
using Newtonsoft.Json;
VB.Net
Imports System.IO
Imports Newtonsoft.Json
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
StreamReader stRead = new StreamReader(Server.MapPath("JSon.txt"));
RootObject root = JsonConvert.DeserializeObject<RootObject>(stRead.ReadToEnd());
}
public class RootObject
{
public int count { get; set; }
public string name { get; set; }
public string frequency { get; set; }
public int version { get; set; }
public bool newdata { get; set; }
public string lastrunstatus { get; set; }
public string lastsuccess { get; set; }
public Result results { get; set; }
}
public class Result
{
public List<Collection1> collection1 { get; set; }
}
public class Collection1
{
public Name name { get; set; }
public string Level { get; set; }
public string Location { get; set; }
}
public class Name
{
public string href { get; set; }
public string text { get; set; }
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim stRead As StreamReader = New StreamReader(Server.MapPath("JSon.txt"))
Dim root As RootObject = JsonConvert.DeserializeObject(Of RootObject)(stRead.ReadToEnd())
End Sub
Public Class RootObject
Public Property count As Integer
Public Property name As String
Public Property frequency As String
Public Property version As Integer
Public Property newdata As Boolean
Public Property lastrunstatus As String
Public Property lastsuccess As String
Public Property results As Result
End Class
Public Class Result
Public Property collection1 As List(Of Collection1)
End Class
Public Class Collection1
Public Property name As Name
Public Property Level As String
Public Property Location As String
End Class
Public Class Name
Public Property href As String
Public Property text As String
End Class