0

Hii Guys!!!. I have a grid which is populating through jason data.Now I want to retrieve the jason values using jquery ajax call from server side code...

Below is my server side code...

getGriddahico.ashx

                        string json ="";
                        json = json + "{\n";
                        json = json + " \"page\":\""+intpage+"\",\n";
                        json = json + "\"total\":"+total_pages+",\n";
                        json = json + "\"records\":"+total+",\n";
                        json = json + "\"rows\": [";
                        rc = false;

                        while(rs.Read()){

                            if(rc){
                                json = json + ",";
                            }
                            json = json + "\n{";
                            json = json + "\"price\":\"" + Convert.ToInt32(rs["price"]) + "\",";
                            json = json + "\"cell\":[" + Convert.ToInt32(rs["price"]) + "";
                            json = json + ",\"" + Convert.ToString(rs["username"]) + "\"";
                            json = json + ",\"" + Convert.ToString(rs["ordinal"]) + "\"";
                            json = json + ",\"" + Convert.ToString(rs["toc"]) + "\"]";
                            json = json + "}";

                            rc=true;
                        }
                        json = json +"]\n";

                        json = json +"}";

and Here is my Jquery Ajax Call code.

 $(document).ready(function () {
        $.getJSON('getGriddahico.ashx', function (data) {
            //loop thru  json data
            //data will contain  json values < --
            $.each(data, function (key, val) {
                console.log(val);
            });
        });
    });

Plz guys Help me how to retrive the value from server side datareader to client side variable.. Thanx in advance.

8
  • 1
    what you have written in you getGriddahico.ashx? Please post it. Commented Jan 30, 2013 at 9:37
  • @krshekhar sir server side code is the 'getGriddahico.ashx' Commented Jan 30, 2013 at 9:38
  • what is rs.Read() is it a data reader (reading data from database)? Commented Jan 30, 2013 at 9:39
  • @krshekhar yes Sir..it is datareader from database Commented Jan 30, 2013 at 9:40
  • @vikas I suggest you to use StringBuilder instead of concatenating strings Commented Jan 30, 2013 at 9:42

2 Answers 2

2

To me it looks as though you are returning a string value which you need to then parse in the JavaScript into a json object.

Use the code $.parseJSON()

$.getJSON('getGriddahico.ashx', function (data) {

    /* convert to json object */
    var json $.parseJSON(data);

    //loop thru  json data
    //data will contain  json values
    $.each(json, function (key, val) {
        console.log(val);
    });
});
Sign up to request clarification or add additional context in comments.

11 Comments

Why some one will have to parse a json into a json? If the content type is json.
How are you returning the json string from getGriddahico.ashx ?
@TimBJames by using HttpContext.Current.Response.Write(json);
@vikas and what is the context.Response.ContentType
@krshekhar sir it is '"text/plain";'
|
0

as you are getting -2147483648 -2147483648 150508 [] the fourth one ([]) is an array.
so you will have to loop again for getting values from the inner array.

You need multiple loop for the same. I think below should work.
In the first loop you will get the values which are not array
and In the inner loop you will get the array values.
Values of page,total and records in the first loop
and in the inner loop you will get price and cell

$(document).ready(function () {
    $.getJSON('getGriddahico.ashx', function (data){ 
        $.each(data, function(idx, obj){ 
           $.each(obj, function(key, value){
                console.log(key + ": " + value);
           });
    });
});

You can refer this link

looping over a json object array with jquery

6 Comments

@krshekar Thnx sir for ur response but here also I am getting only random values like 0:- 1:2 2:1 3:4 etc
can you put your json created by c#
@krshekar Here is my json values Sir... { "page":"1", "total":1506, "records":150508, "rows": [ {"price":"0","cell":[0,"","3011","0","3011","9999","1784","6/13/2011 12:00:00 AM","1/1/1900 1:22:17 PM","63","","0","INTERNAL"]}, {"price":"0","cell":[0,"","1229","0","1229","9999","1255","6/13/2011 12:00:00 AM","1/1/1900 1:26:23 PM","2","","0","INTERNAL"]}] }
what do you want to do with these values?
These values are generated at server i want to retrieve these values to .aspx client page..by jquery url call
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.