2

I am very new to json, please help me out! Suppose I have the following json object

{
    "Table": "",
    "Id": "",
    "Column": [
        {
            "ColumnText": "",
            "ColumnSqlName": ""
        },
        {
            "ColumnText": "",
            "ColumnSqlName": ""
        }
    ]
}

I want to use asp.net to deserialize the above json, what is the best way to do this? Thanks!!!

1
  • mark at least one solution as accepted, if issue resolved.. Commented Jun 6, 2012 at 12:54

4 Answers 4

2

I recommend to use the JSON.NET library to serialize and deserialize the objects into json

For Example: it deserialize the json object into C# object...

string json = @"{
  ""Name"": ""Apple"",
  ""Expiry"": new Date(1230422400000),
  ""Price"": 3.99,
  ""Sizes"": [
    ""Small"",
    ""Medium"",
    ""Large""
  ]
}";

JObject o = JObject.Parse(json);

string name = (string)o["Name"];
// Apple

JArray sizes = (JArray)o["Sizes"];

string smallest = (string)sizes[0];
// Small

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

MSDN has a simple example for you to follow. If it is a service, it may be easier to simply consume it using WCF though.

Comments

1

You can use the built in class JavaScriptSerializer this. MyClass is a class matching the json structure

var serializer = new JavaScriptSerializer();
MyClass instance = serializer.Deserialize<MyClass>(myJson);

Comments

1
    Public Class JsonObj
    Private _Table As String
    Public Property Table() As String
        Get
            Return _Table
        End Get
        Set(ByVal value As String)
            _Table = value
        End Set
    End Property
    Private _Id As String
    Public Property Id() As String
        Get
            Return _Id
        End Get
        Set(ByVal value As String)
            _Id = value
        End Set
    End Property

    Private _Column As JsonSetting()
    Public Property Column() As JsonSetting()
        Get
            Return _Column
        End Get
        Set(ByVal value As JsonSetting())
            _Column = value
        End Set
    End Property
End Class

Public Class JsonSetting
    Private _ColumnText As String
    Public Property ColumnText() As String
        Get
            Return _ColumnText
        End Get
        Set(ByVal value As String)
            _ColumnText = value
        End Set
    End Property

    Private _ColumnSqlName As String
    Public Property ColumnSqlName() As String
        Get
            Return _ColumnSqlName
        End Get
        Set(ByVal value As String)
            _ColumnSqlName = value
        End Set
    End Property
End Class

Usage:

Dim o As New JavaScriptSerializer
Dim instance As JsonObj = o.Deserialize(Of JsonObj)(json_str)

Thanks for all of your reply, but the above code may work great.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.