user1314159 user1314159 - 6 days ago 6
jQuery Question

parse a JSON string using Ajax and javascript

I have a vb.net webservice that produces a JSON string that I process using AJAX and JavaScript. Currently I build the string using Newtonsoft.Json and the JSON comes out looking like :

[{"NAME":"Richard","Description":"Richard

and I parse it using

success: function (data) {
var = dataJson = JSON.parse(data.d);


from my Success Function. I started trying JsonConvert.SerializeObject(ds) and it comes out like:

{"Results":[{"NAME":"Richard ","Description":"Richard.

My dataJson = JSON.parse(data.d);

is tripping on the "Results" I understand that I'm sterilizing a dataset on the server but need to know how to read the first results.
Something like:

dataJson = JSON.parse(data.d);
var results = dataJson.[0] ?????

Answer

First of all, you have in you question code the following that I hope is just a typo:

var = dataJson = JSON.parse(data.d); 

When it supposed to be:

var dataJson = JSON.parse(data.d); 

Second, when you serialize an object (or list, or array) in your backend, and de-serialize in Javascript, you are going to have the same object, just that now in javascript, so if is an array or list, you will be accesing the first element like this:

var results = dataJson[0];

Or:

var results = dataJson[0].something;

if you want to access some property.

Comments