views:

197

answers:

5

Hello all, and thanks for reading.

I wrote a web service which is called through jsonp, and returns a .Net 3.5 DataSet which I then want to parse using jquery. It appears that the getJson() method is working fine as far as parsing the json goes. The following request -

            $.getJSON("http://localhost:80/ws.asmx/Example?format=json&callback=?", function(data) {
            alert(data.d);
        });

results in -

{"Tables": [

{

  "Rows": [

    {

      "CASE_TYP_CD": "M",

      "CASE_TYP_DESC": "MOVING VIOLATION",

      "AUTO_GENERATE": "Y",

      "CONFIDENTIAL_FLG": "N"

    },

    {

      "CASE_TYP_CD": "T",

      "CASE_TYP_DESC": "TRUANCY/FAILURE TO ATTEND SCHOOL",

      "AUTO_GENERATE": "Y",

      "CONFIDENTIAL_FLG": "N"

    },

    {

      "CASE_TYP_CD": "J",

      "CASE_TYP_DESC": "JUVENILE",

      "AUTO_GENERATE": "Y",

      "CONFIDENTIAL_FLG": "N"

    }...

Well I'm kind of new at this whole json thing and I can't figure out how to gain access to the values held in the nodes.

I was thinking it would be something like -

$.each("CASE_TYP_CD", function() { //code goes here });

But that doesn't seem to be working.

Could anyone help me out with this?

Thanks in advance!

+1  A: 

Read this: http://api.jquery.com/jQuery.getJSON/

xyld
Thank you I already have been through that page. I must be missing something because if I try alert(data.CASE_TYP_CD[0]) I recieve a javascript error that CASE_TYP_CD.0 is undefined.
Zachary Carter
A: 

You may want to read up on general JavaScript object literals (which are very similar to JSON).

What I see from JSON, to get the value of CASE_TYP_CD in the first row of the first table you write

var result = <json>;
var val result.Table[0].Rows[0].CASE_TYP_CD;

is that what you wanted to know?

Kathy Van Stone
A: 

here's how you would iterate through your object with $.each():

$.each(data.d.Tables, function(index, table) {
    $.each(table.Rows, function(index, row) {
        alert(row.CASE_TYP_CD);
    });
});
ob
A: 

I'm pretty sure something is up with the way the DataSet is being serialized in my web service.

Whenever I try a method, including the ones that have been suggested here I get two javascript errors -

Message: 'length' is null or not an object
Line: 29
Char: 452
Code: 0
URI: js/jquery-1.4.2.min.js

and

Message: Object doesn't support this property or method
Line: 1
Char: 1
Code: 0
URI: /GetCaseTypes?format=json&callback=jsonp1273246775675

Thank you for all your suggestions but it looks like my problem lies within the serialization of the DataSet and not in my jquery code.

Zachary Carter
A: 

I figured out the problem, apparently the getJson method wasn't actually parsing the json string into a java object like it should have been. I simply had to add

var obj = $.parseJSON(data.d);
alert(obj.Table[0].CASE_TYP_CD);

and everything worked as expected.

Thanks to everyone who posted here.

Zachary Carter