0

I want to parse JSON string in C# asp.net mVC3 but not getting idea of how to parse my json string. my JSON String is like that:

{"dept":"HR","data":[{"height":5.5,"weight":55.5},{"height":5.4,"weight":59.5},{"height":5.3,"weight":67.7},{"height":5.1,"weight":45.5}]}

Code:

var allData = { dept: deptname, data: arr};
var allDataJson = JSON.stringify(allData);
$.ajax({
                url: '<%=Url.Action("funx","Controller")%>',
                data: { DJson: allDataJson },
                async: false,
                cache: false,
                type: 'POST',
                success: function (data) {
                    alert("success data: "+data);
                }

}); 

public String funx(string DJson)
{
     System.Diagnostics.Debug.WriteLine("Returned Json String:" + DJson);

     // var yourObject = new JavaScriptSerializer().Deserialize(DJson);
     return "successfull";
}

I am new to asp.net. I want to parse the string and save it in database.

1
  • Use JSON.NET for parsing your json Commented Aug 20, 2015 at 6:33

3 Answers 3

1

Method 1:

Create two classes with the structure of your JSON string:

public class myobj
{
    public string dept;
    public IEnumerable<mydata>;
}

public class mydata
{
    public int weight;
    public int height;
}

and after that use this to parse it:

public static T FromJSON<T>(string str)
{
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    return serializer.Deserialize<T>(str);
}

Like this:

myobj obj = MP3_SIOP_LT.Code.Helpers.JSONHelper.FromJSON<myobj>(@"{""dept"":""HR"",""data"":[{""height"":5.5,""weight"":55.5},{""height"":5.4,""weight"":59.5},{""height"":5.3,""weight"":67.7},{""height"":5.1,""weight"":45.5}]}");

The result:

enter image description here

Method 2:

If you don't want to have classes with your JSON structure, use the same method as above like this but in order to get a dynamic object:

dynamic obj = MP3_SIOP_LT.Code.Helpers.JSONHelper.FromJSON<dynamic>(@"{""dept"":""HR"",""data"":[{""height"":5.5,""weight"":55.5},{""height"":5.4,""weight"":59.5},{""height"":5.3,""weight"":67.7},{""height"":5.1,""weight"":45.5}]}");

The result:

enter image description here

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

Comments

0

First create a model for your json

public class Size
{
    public double height { get; set; }
    public double weight { get; set; }
}

public class MyData
{
    public string dept { get; set; }
    public List<Size> data { get; set; }
}

Now you can deserialize your json. With built-in DataContractJsonSerializer

var serializer = new DataContractJsonSerializer(typeof(MyData));
var data = (MyData)serializer.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(json)));

or With Json.Net

var data = JsonConvert.DeserializeObject<MyData>(json);

You can even go the dynamic way without creating any classes

dynamic dynObj = JsonConvert.DeserializeObject(json);

foreach(var item in dynObj.data)
{
    Console.WriteLine("{0} {1}", item.height, item.weight);
}

Comments

0

You can use NewtonSoft Json.Net for parsing.

Try this

var json = "{\"dept\":\"HR\",\"data\":[{\"height\":5.5,\"weight\":55.5},{\"height\":5.4,\"weight\":59.5},{\"height\":5.3,\"weight\":67.7},{\"height\":5.1,\"weight\":45.5}]}";
var foo = JsonConvert.DeserializeObject<RootObject>(json);

// Check Values
// var department = foo.dept;
// foreach (var item in foo.data)
// {
//      var height = item.height;
//      var weight = item.weight;
// }

public class Datum
{
    public double height { get; set; }
    public double weight { get; set; }
}

public class RootObject
{
    public string dept { get; set; }
    public List<Datum> data { get; set; }
}

Nuget: Json.Net

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.