Skip to content Skip to sidebar Skip to footer

How Do I Pre Parse A JSON String?

I want to feed this JSON URL to another site http://naturetrek.co.uk/blog/api/get_recent_posts/ But the problem is that because of the initial three

tags its not valid JS

Solution 1:

$.getJSON is just shorthand for:

$.ajax({
  dataType: "json",
  url: url,
  data: data,
  success: success
});

See documentation here: http://api.jquery.com/jQuery.getJSON/

Just use $.ajax change dataType to "html" and parse your modified JSON data in the success function with $.parseJSON

Note: Since it seems you are doing a cross-domain request, you must use jsonp, read more here:

http://learn.jquery.com/ajax/working-with-jsonp/


Solution 2:

You should REALLY ask them to fix their JSON...

In the meantime, as a patch, you should download it as HTML content, preparse it (by removing the tags) and then parse the resulting content as plain JSON.

But the above procedure is really a patch that should disappear when the JSON producers fixes it.


Post a Comment for "How Do I Pre Parse A JSON String?"