开发者

Not able to parse json in jquery

I'm using jquery.parseJSON() but it is not happening. What I'm doing is making an ajax call to server and then on success I try to parse data got from the server using jquery.parseJSON() but it is not working.

function getIdVals(id){
        $.ajax(
            {
                url: "MyServlet",
                data: "Id="+id,
                cache: false,
                success: function(html){
                    alert(html);
                    var obj = jquery.parseJSON(html);
                    alert(obj.data);
                }
            }
        );
    }

The re开发者_如何转开发sponse I got from server is something like below:

'{ "data" : "{ aas:five,asda:five,alskjaskdakbd:two,test:two,asddas:five,
             lasnd:five,ad:five,this:two,smd:five,alskjaskdakbdals:four,}"}'

When I try to put the above string in jquery.parseJSON function it works fine but don't know why writing it as jquery.parseJSON(html) doesn't work.

How can I solve this?


the json you are getting is in valid it should be something like

{
    "data": {
        "aas": "five",
        "asda": "five",
        "alskjaskdakbd": "two",
        "test": "two",
        "asddas": "five",
        "lasnd": "five",
        "ad": "five",
        "this": "two",
        "smd": "five",
        "alskjaskdakbdals": "four"
    }
}

and then as @zerkms answered

function getIdVals(id){
        $.ajax(
            {
                url: "MyServlet",
                data: "Id="+id,
                cache: false,
                dataType: 'json', // <<<<--------
                success: function(json){
                    // work with json here                    
                }
            }
        );
    }


You could specify dataType: 'json' attribute to the request, and you'll get parsed object in your function instead

function getIdVals(id){
        $.ajax(
            {
                url: "MyServlet",
                data: "Id="+id,
                cache: false,
                dataType: 'json', // <<<<--------
                success: function(json){
                    // work with json here                    
                }
            }
        );
    }


Your json is not correct. The bugs in json are:

'{ "data" : "{ aas:five,asda:five,alskjaskdakbd:two,test:two,asddas:five,
             lasnd:five,ad:five,this:two,smd:five,alskjaskdakbdals:four,}"}'
  1. single quotes at start and at the end.
  2. comma at the end of value.
  3. It seems like you also want to get json data in value. So for that also wrap entries in value by quotes.

So a valid json will look like:

{ "data" : { "aas":"five","asda":"five","alskjaskdakbd":"two","test":"two",
            "asddas":"five","lasnd":"five","ad":"five","this":"two","smd":"five",
            "alskjaskdakbdals":"four"}}

Try to get this kind of json from server.

As far as jquery ajax call is concern I don't think any change is required if you are just getting this json string as the response on success function and if you are getting this as a part of response with some stuff then others have well explained it.


For your comment on 3nigma's answer:

    $.each(obj.data, function(key,val) {
        alert(key+" "+val);
    });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜