Modify your webpage to exclusively generate JSON:
{"groups":["1210103","1210103","1210103","1210405"],
"prices":["279,00","399,00","628,00","129,00"],
"titles":["","","",""]
}
Remember, when working with JSON, always use "
for string quoting, not '
.
Ensure the Content-Type
header of your response is set to application/json
. If you encounter issues setting the header, you can specify dataType: 'json'
in your jQuery ajax
call to interpret the response as JSON, although it's preferable to set the correct Content-Type
header.
Following this, in the success
callback of your ajax
call, the result
will already be a deserialized object with three properties (groups
, prices
, titles
), presented as JavaScript arrays for manipulation.
See Live Example | View Source
As mentioned in the comments, your page is a complete HTML page embedded with a script
tag, and you have limited control over its content due to the CMS in use.
It is highly recommended to consider transitioning to a more adaptable CMS.
If transitioning is not an immediate option, you can acquire the page as text and parse the JSON. Revise your script
tag as seen below:
<script>
var JSONObject = /*XXX_JSONSTART_XXX*/{"groups":["1210103","1210103","1210103","1210405"],
"prices":["279,00","399,00","628,00","129,00"],
"titles":["","","",""]
}/*XXX_JSONEND_XXX*/;
</script>
Utilize the markers to extract the JSON section between them, and implement $.parseJSON
for processing. Here's an example:
(function($) {
$.ajax({
url: "http://jsbin.com/ecolok/1",
dataType: "text",
success: function(result) {
var startMarker = "/*XXX_JSONSTART_XXX*/";
var endMarker = "/*XXX_JSONEND_XXX*/";
var start, end;
start = result.indexOf(startMarker);
if (start === -1) {
display("Start marker missing");
}
else {
start += startMarker.length;
end = result.indexOf(endMarker, start);
if (end === -1) {
display("End marker missing");
}
else {
result = $.parseJSON(result.substring(start, end));
display("result.groups.length = " + result.groups.length);
display("result.prices.length = " + result.prices.length);
display("result.titles.length = " + result.titles.length);
}
}
}
});
function display(msg) {
$("<p>").html(String(msg)).appendTo(document.body);
}
})(jQuery);
View Live Copy | View Source