I'm having a problem with getting json data. I created 2 examples. One works, but the other doesn't. And guess which one I need? Yup, the one that doesn't work. Here is the code.
I have a web service that outputs json data but for some reason it add extra brackets [] to the string and it's also missing the single quotes ' '. If you look at the code that doens't work, you'll see that i'm manually removing the brackets and adding the single quotes. I have a div that i write the string to and it's valid Json data. If I take that string and manually declare a new variable with it, the jQuery.parseJSON works fine. But it I parse the newly created object, it doesn't work. Anybody have any ideas?
Works Fine
$.ajax({
type: "POST",
async: false,
contentType: "application/json; charset=utf-8",
url: "StudiesWebService.asmx/EventList",
data: "{}",
dataType: "json",
success: function(msg) {
var obj = jQuery.parseJSON('{ "id": 1, "title": "Jack STuff", "start": "\/Date(1318939200000)\/", "end": "\/Date(1318950000000)\/", "allDay": false }, { "id": 2, "title": "asdfasdfasdf", "start": "\/Date(1319025600000)\/", "end": "\/Date(1319025600000)\/", "allDay": false}');
var events2 = [];
events2.push({
title: obj.title,
allDay: obj.allDay,
start: 'Tue, 18 Oct 2011 10:00:00 EST',
end: 'Tue, 18 Oct 2011 11:00:00 EST'
});
callback(events2);
},
error: function(e) { $(".external-events").html("An Error Occured" + e); }
});
Doesn't Work:
$.ajax({
type: "POST",
async: false,
contentType: "application/json; charset=utf-8",
url: "StudiesWebService.asmx/EventList",
data: "{}",
dataType: "json",
success: function(msg) {
var myObj = new String(msg.d);
myObj = myObj.replace("[", "");
myObj = myObj.replace("]", "");
myObj = "'" + myObj + "'";
//at this point myObj output to this:
//'{"id":1,"title":"Mike STuff","start":"\/Date(1318939200000)\/","end":"\/Date(1318950000000)\/","allDay":false},{"id":2,"title":"asdfasdfasdf","start":"\/Date(1319025600000)\/","end":"\/Date(1319025600000)\/","allDay":false}'
var obj1 = jQuery.parseJSON(myObj);
alert(obj1.id); //alert doesn't come up
var events = [];
events.push({
title: obj1.title,
allDay: obj1.allDay,
start: 'Tue, 18 Oct 2011 10:00:00 EST',
end: 'Tue, 18 Oct 2011 11:00:00 EST'
});
callback(events);
},
error: function(e) { $(".external-events").html("An Error Occured" + e); }
});
-
Thank you everyone for your help. You not only fixed this error but a few others that I had in my code. I just started working with javascript and jquery recently and I'm having a tough time. Coming from a vb.net environment and learning c# and jquery is kicking my behind.MikeB55– MikeB552011年10月20日 16:47:14 +00:00Commented Oct 20, 2011 at 16:47
-
Do you control the webservice that's spitting out the bad json? If you do, why not fix that rather than trying to compensate here?Marc B– Marc B2011年10月20日 16:51:36 +00:00Commented Oct 20, 2011 at 16:51
-
Yes, this is my web service. Not sure what I'm doing wrong here either: var eventList = from e in _db.events select new { id = e.event_id, title = e.title, start = e.from_date, end = e.to_date, allDay = false }; var rows = eventList.ToArray(); JavaScriptSerializer jss = new JavaScriptSerializer(); string strJSON = jss.Serialize(rows); return strJSON;MikeB55– MikeB552011年10月20日 16:53:47 +00:00Commented Oct 20, 2011 at 16:53
5 Answers 5
if you remove this line
myObj = "'" + myObj + "'";
it should be fine. In the upper example, the '' enclose the string. they are not actually part of the string itself, so they do not need to be added to myObj
Also, your json has [] around it because it's actually returning a 2-element array ([{obj1},{obj2}]), so you should either iterate that array, or pull out the first element (obj[0])
6 Comments
jQuery.parseJSON(myObj)[0];Adding the single quotes around the whole object would turn it into a single string... so you probably don't mean to do that.
I'm not sure where the brackets are that you're removing, but you probably want to keep those because the objects within them will be treated as an array.
Comments
Why are you removing the
[and the]? Your turning something which is valid JSON into something that's invalid.The response you're getting is an array (denoted by the
[and]) of objects (denoted by{and}).You'd get away calling jQuery.parseJSON on the input as you receive it, however because you've set the
dataType: "json", jQuery parses it for you, sodatashould be the array of objects you're after.You shouldn't be adding the
''s around the string.data: "{}"is meaningless. You want it to either be a string of key=value pairs (foo=1&bar=2, like a query string), or an object ({foo:1, bar:2}), which jQuery turns into a jQuery string for you. In your case, you can omit thedataattribute completely.
1 Comment
The problem is the following
myObj = "'" + myObj + "'";
JSON should not be surrounded by quotes.
You are also remove the [ ] characters, which will make it an array. However, as it stands, you'll want to split it and evaluate each piece instead of trying to process it all, because a raw array is not valid JSON.
4 Comments
[ and ], because he's receiving an array of objects.[{ /* object */ }, { /* object */ }] into { /* object */ }, { /* object */ }Check out jsonlint.com to verify that your json is properly formatted before you try to parse it. The string you have in your comment under the "not working" version is not properly formatted - it needs to be wrapped in "[...]".