views:

435

answers:

4

Can anyone shed any light as to why my JSON is coming out below, with the extra backslashes. I am using ASP.net MVC to serialise a datatable, when I debug in Visual studio it all looks ok but when I look with firebug with adds the extra characters?

Any ideas anyone?

"[{\"uid\":\"516219026\",\"pic\":\"http://profile.ak.net/\",\"first_name\":\"Daniel\",\"last_name\":\"James\",\"fql_query_response_Id\":0,\"LIFEID\":null}


function GetFBFriends() {
    FB.Connect.requireSession(function() {
        $.ajax({
            url: "/Facebook/GetFaceBookFriends",
            type: 'POST',
            data: null,
            dataType: 'json',
            success: function(result) {
                data = "<table>";
                alert(result.length);
                for (i = 0; i < result.length; i++) {
                    data += "<tr><td><td><img src=" + result[i].pic + " alt=" + result[i].first_name + " /></td><input type='checkbox' value='" + result[i].uid + "' name='friends[]' id = 'friend" + result[i].uid + "' /></td><td>" + result[i].first_name + " " + result[i].last_name + "</td></tr>";
                }
                data += "</table>"; ;
            }
        });
    })
};






Public Function GetFaceBookFriends() As JsonResult
            Dim fbFriends As New DataTable
            Try
                fbFriends = FacebookModel.GetFriendsAndMatchToLife()
                Return Json(JsonConvert.SerializeObject(fbFriends))
            Catch ex As Exception
            Finally
                fbFriends.Dispose()
                fbFriends = Nothing
            End Try
        End Function
+1  A: 

Looks like Firebug is adding escape characters. What if you enclosed your entire JSON in single quotes? That may correct the problem. Edit Can you provide the code that encodes your JSON?

Urda
+3  A: 

That's Firebug showing the string containing JSON in it's string representation. Think of it as JSON-encoding a string containing JSON. Or rather, if your were to put the JSON in a string literal in your Javascript, it would look like that.

Your string does not actually contain those backslashes. They are just escapes for the double-quotes.

Shtééf
It is seeing those backslashes as the JSON is splitting into 300 characters instead of two rows.
DanielJaymes
That JSON looks fine, so perhaps something else is going wrong? Can you give an example of how you're trying to use the JSON?
Shtééf
I've just added the JS to the original question.
DanielJaymes
A: 

I would suggest doing injecting the following into the first line for the success function.

console.dir({'result':result});

This will show you what you are getting back, as opposed to just viewing the result from the network call.

The Firebug display is simply escaping the string, so you can copy/paste the entire result into the console for inspection/interrogation directly...

var temp = {pasted-string-here}
//var temp = "[{\"uid\":\"516219026\",\"pic\":\"http://profile.ak.net/\", ... }]"
var val = JSON.parse(temp);
console.debug({"val":val});
Tracker1
"[{"uid":"516219026","pi...e_Id":0,"LIFEID":null}]"This is what I am getting back?
DanielJaymes
yes, that's what you are actually getting back. The firebug UI is simply escaping the internal quotes for you, so you can cut/paste into the js console.
Tracker1
A: 

I solved this question, I was returning JSON data which was then being changed into JSON by jquery as well, so I simply returned a string and jquery handled it correctly.

DanielJaymes