I have a following JSON string, arriving via AJAX to server:
{"Names":"[{0:'asdasd'}]","Values":"[{0:'ad'}]"}
As you see, Names and Values were intended to hold an array. Problem is, when I call
$data = json_decode(stripslashes($_POST['data']), true);
$data['Names'][0] I don't get 'asdasd' as I wanted, but "[" symbol. Where the problem lies?
P.S. JS code, sending JSON string:
var arr_names = "[";
names.each(function(i){
arr_names += "{" + i + ":'" + $(this).val() + "'}";
if (i < names.length-1) arr_names += ",";
});
arr_names += "]";
var arr_val = "[";
values.each(function(i){
arr_val += "{" + i + ":'" + $(this).val() + "'}";
if (i < values.length-1) arr_val += ",";
});
arr_val += "]";
var el = { "Names" : arr_names, "Values" : arr_val };
el = encodeURIComponent(JSON.stringify(el));
$.ajax({
type:"POST",
dataType:"html",
data:"m=1&t="+type+"&data="+el,
url:plugin_path+"option-proc.php",
success: function(rsp){
$("#result").html(rsp);
}
});
names and values are a bunch of text fields, selected by the class. m and t variables being sent, are completely irrelevant to the case :)
{"Names": [{"0":"asdasd"}] ,"Values":[{"0":"ad"}]}- no quotes around the arrays, all property names must be quoted, and all quotes must be " and not ' quotes.