0

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 :)

15
  • 1
    That JSON is not really correct. The arrays should probably not be encoded as strings like that, because it'll force you to have to do multiple decode passes. Commented Oct 2, 2011 at 12:49
  • Could you give a link to some best practices guides? Commented Oct 2, 2011 at 12:53
  • Best practises: 1. Write JSON, not JavaScript. 2. Encode your data using JSON. Don't serialise things JSON has built into strings first. Commented Oct 2, 2011 at 12:54
  • 1. Write JSON, not JavaScript. - what do you mean by it? I need to build a JSON string. Dynamically. Commented Oct 2, 2011 at 13:00
  • 1
    Your JSON should look like: {"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. Commented Oct 2, 2011 at 13:03

1 Answer 1

2

The string is encoded incorrectly. $data['Names'] is a string, so by accessing [0] you'll get the first character.

If you also json_decode $data['Names'] again you should get something working, although also that is actually incorrectly ecoded (as an object with numeric indexes rather than an array.) I'm pretty sure strict json parsers will fail on that inner-string.

I'd suggest fixing whatever generates it, rather than on the decoding side.

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

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.