2

I'm using php json_encode to send a bunch of values to a client, where jquery

{"data":{"application":{"basics":{"email":"[email protected]","name_first":"Step","name_last":"Bob","phone":"9210938102938","occupation":"unemployed","website":"wwwcom"},"application":{"title":"Default title","guid":"9as8ua9sd8ua9sdu","language":"sv"},"job":{"title":"joburl","url":"joburl"},"letter":{"letter":"abadbabkbakbakbakbakbakbabk"},"work-experiences":[{"title":"Default WORK Experience Title","url":"wwwsscom","date_from":"1970-01-01","date_to":"1999-12-31"},{"title":"Default WORK Experience Title","url":"wwwsscom","date_from":"1970-01-01","date_to":"1999-12-31"},{"title":"Default WORK Experience Title","url":"wwwsscom","date_from":"1970-01-01","date_to":"1999-12-31"}],"educations":{"title":"Default Education","url":"ixi","date_from":"1970-01-01","date_to":"1999-12-31"},"skills":{"title":"Defailt SKILL","url":"wwwsdcom","date_from":"ixi","date_to":"ixi"},"work-samples":{"title":"A sample of my work","url":"wwwsdcom","date_from":"ixi","date_to":"1999-12-31"}}},"error":[],"warning":[]}

If I try to parse this with $.parseJSON in a script I get no object. However, if I copy/paste it directly into the console (and add ' in the beginning and end), it works. There are no error codes and I can see no linebreaks. JSON lint-tools returns no errors...

I have set the correct content type and tried the different json parsers that jquery provides.

What have I missed?


The code was cut/pasted from the jQuery tutorials. I tried some different examples but they all failed.

var jqxhr = $.getJSON( "application_controller.php", function() {
    console.log( "success" );
})
.done(function() {
    console.log( "second success" );
})
.fail(function() {
    console.log( "error" );
})
.always(function(data) {
    console.log( "complete" );
    application = data;  
});

// Perform other work here ...

// Set another completion function for the request above
jqxhr.complete(function() {
    console.log( "second complete" );
});
});

It's true, I decoded it in the console. this is a snippet that does in in-script (it also won't work):

$.ajax({
  dataType: "json",
  contentType: "application/json",
  url: 'application_controller.php',
  data: '{id:id}',
  success: function( data ) {
        application = data.responseText;
        application = $.parseJSON(application);/* < string */     
    },
    fail: console.log("fail"),
    complete: function(data) {
        console.log(data.responseText);
        application = data.responseText;
    }
});
3
  • Show us the ajax function where you're trying to parse it Commented Dec 21, 2013 at 19:15
  • As a sidenote, if you've set the dataType to JSON, it's already parsed, and parsing it again will cause an error. Commented Dec 21, 2013 at 19:15
  • You're using getJSON, so the JSON is already parsed, but it's also asynchronous, and where exactly are you trying to do this parsing ? Commented Dec 21, 2013 at 19:28

2 Answers 2

1

Answering my own question:

To solve this error I had to change all my php files from "utf8" encoding to "utf8 without BOM". Then it worked.

When a file without utf8 encoding was included (low in the hierarchy of course) it tainted all the other files and corrupted the output.

Sign up to request clarification or add additional context in comments.

Comments

0

use JSON.parse(jsonString) function.

var jsonString = '{"data":{"application":{"basics":{"email":"[email protected]","name_first":"Step","name_last":"Bob","phone":"9210938102938","occupation":"unemployed","website":"wwwcom"},"application":{"title":"Default title","guid":"9as8ua9sd8ua9sdu","language":"sv"},"job":{"title":"joburl","url":"joburl"},"letter":{"letter":"abadbabkbakbakbakbakbakbabk"},"work-experiences":[{"title":"Default WORK Experience Title","url":"wwwsscom","date_from":"1970-01-01","date_to":"1999-12-31"},{"title":"Default WORK Experience Title","url":"wwwsscom","date_from":"1970-01-01","date_to":"1999-12-31"},{"title":"Default WORK Experience Title","url":"wwwsscom","date_from":"1970-01-01","date_to":"1999-12-31"}],"educations":{"title":"Default Education","url":"ixi","date_from":"1970-01-01","date_to":"1999-12-31"},"skills":{"title":"Defailt SKILL","url":"wwwsdcom","date_from":"ixi","date_to":"ixi"},"work-samples":{"title":"A sample of my work","url":"wwwsdcom","date_from":"ixi","date_to":"1999-12-31"}}},"error":[],"warning":[]}';

var myData = JSON.parse(jsonString);

Fiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.