Click here to Skip to main content
15,890,399 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
C#
{"delivery_codes": [{"postal_code": {"district": "Bellary", "pin": 583217, "max_amount": 0.0, "pre_paid": "N", "cash": "N", "pickup": "N", "repl": "N", "cod": "N", "sort_code": null, "is_oda": "N", "state_code": "KA", "max_weight": 0.0}}]}


What I have tried:

Can anybody tell me how to deserialize this and display it?
Posted
Updated 9-Dec-16 9:14am
Comments
Mehdi Gholam 9-Dec-16 2:22am    
What have you tried?
santoshpm45 9-Dec-16 2:25am    
i have tried
var jss = new JavaScriptSerializer();
var dict = jss.Deserialize<dynamic>(result);
Response.Write(dict["delivery_codes"]["postal_code"]);
not getting the result
Karthik_Mahalingam 12-Dec-16 4:30am    
Always use  Reply  button, to post Comments/query to the user, so that the user gets notified and responds to your text.

Below solution works if you iterating json on page itself.

<script>
    $(function () {
        var _Obj = { "delivery_codes": [{ "postal_code": { "district": "Bellary", "pin": 583217, "max_amount": 0.0, "pre_paid": "N", "cash": "N", "pickup": "N", "repl": "N", "cod": "N", "sort_code": null, "is_oda": "N", "state_code": "KA", "max_weight": 0.0 } }] };
        var _tmp = "";
        $.each(_Obj["delivery_codes"][0]["postal_code"],function (i, val) {
            _tmp += "<li>" + i + "=" + val + "</li>"
        });
        $("#list").html("<ul>" + _tmp + "/<ul>");
    });
</script>
 
Share this answer
 
Comments
santoshpm45 9-Dec-16 5:17am    
This one works fine thank you
Did you Google for?
We have even an article on this very topic, here at Code Project: JSON Serialization and Deserialization in ASP.NET[^].
 
Share this answer
 
Hi,

Try Json.NET - Newtonsoft[^] with below sample code.

C#
string strJSON = "{'delivery_codes': [{'postal_code': {'district': 'Bellary', 'pin': 583217, 'max_amount': 0.0, 'pre_paid': 'N', 'cash': 'N', 'pickup': 'N', 'repl': 'N', 'cod': 'N', 'sort_code': null, 'is_oda': 'N', 'state_code': 'KA', 'max_weight': 0.0}}]}";


            JavaScriptSerializer ser = new JavaScriptSerializer();

            var objS = ser.Deserialize<object>(strJSON);
 
Share this answer
 
Plus, this is a general recommendation that to parse this kind of JSON, you should consider using a dynamic approach. That way you can overcome maximum of errors in deserialization of your JSON document.

I wrote the article about this topic in C#, From zero to hero in JSON with C#[^], there was a question similar to this that I answered on the same forum there[^]. This will give a very good answer to solving these kind of problems.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900