Nishant Kumar
Nishant Kumar

Reputation: 6083

Parse Json Data by javascript or Jquery on client side

I want to parse data on client side, I am keeping data in input field after serialization.

JavaScriptSerializer objJavaScriptSerializer = new JavaScriptSerializer();
string jsonString = objJavaScriptSerializer.Serialize(_statusVal);
jsonFmtStatusValue.Value = jsonString;

On Client Side When I saw the data string which is stored in input field it's coming like this

[
    {
        "nodeContentId": "1234_5678",
        "statusTypeId": "5678",
        "statusName": "Submitted by Customer",
        "dateTime": "/Date(1352745000000)/",
        "forceEmail": "on",
        "user": {
            "userId": "0",
            "userName": "admin"
        },
        "note": {
            "typeName": null,
            "dataValue": null
        }
    },
    {
        "nodeContentId": "1234_5678",
        "statusTypeId": "5678",
        "statusName": "Checked, Printed, Folded & Re-checked",
        "dateTime": "/Date(1353402060000)/",
        "forceEmail": "on",
        "user": {
            "userId": "0",
            "userName": "admin"
        },
        "note": {
            "typeName": null,
            "dataValue": null
        }
    }
]

Code Which I tried to parse Json data is :

    var JsonData = $("#<%=jsonFmtStatusValue.ClientID %>").val();
    obj = jQuery.parseJSON(JsonData)
    alert(obj.nodeContentId);

Things I get in alert box: Undefine

Not able to figure out what should i use for parsing.

Upvotes: 0

Views: 3931

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074258

(Note: I'm assuming that jsonFmtStatusValue ends up being an input or textarea on the page.)

In your alert(obj.nodeContentId);, obj is the array, not an object in the array. Your outermost JSON entity is an array, which then contains objects.

You can see the first nodeContentId like this:

alert(obj[0].nodeContentId);

...and of course the others are at subsequent indexes, so for instance:

var obj = jQuery.parseJSON(JsonData);
var n;
for (n = 0; n < obj.length; ++n) {
    alert("obj[" + n + "].nodeContentId = " + obj[n].nodeContentId);
}

Upvotes: 4

Related Questions