为什么这个json字符串在解码后不打印

时间:2016-12-26 06:32:13

标签: php json

我有这样的json,

[{
    "element": "<div class="
    fb - autocomplete form - group field - autocomplete - 1482728916013 - preview "><label for="
    autocomplete - 1482728916013 - preview " class="
    fb - autocomplete - label ">Autocomplete  </label> <input type="
    autocomplete " class="
    autocomplete " name="
    autocomplete - 1482728916013 - preview " id="
    autocomplete - 1482728916013 - preview "></div>"
}, {
    "element": "<div class="
    fb - button form - group field - button - 1482728916826 - preview "><button type="
    button " class="
    button - input btn -
    default btn " name="
    button - 1482728916826 - preview " style="
    default " id="
    button - 1482728916826 - preview ">Button</button></div>"
}, {
    "element": "<div class="
    fb - checkbox form - group field - checkbox - 1482728917611 - preview "><input type="
    checkbox " class="
    checkbox " name="
    checkbox - 1482728917611 - preview " id="
    checkbox - 1482728917611 - preview "> <label for="
    checkbox - 1482728917611 - preview " class="
    fb - checkbox - label ">Checkbox  </label></div>"
}, {
    "element": "<div class="
    fb - date form - group field - date - 1482728918827 - preview "><label for="
    date - 1482728918827 - preview " class="
    fb - date - label ">Date Field  </label> <input type="
    date " class="
    calendar " name="
    date - 1482728918827 - preview " id="
    date - 1482728918827 - preview "></div>"
}]

编码数组后,我将其保存到数据库,如

$element = json_encode($element_array);  // next i saved it to the database

我的json(i,e $element)与上面的json类似。这个json中的错误是什么?

这是我的演示,为什么没有打印?演示:http://phpio.net/s/2bsw

问题:在encoding之后我将json保存到数据库但是我没有得到任何输出,看起来这个json尽管json_encoding()不是有效的。为什么?

在将json存储到数据库之前对json进行编码的最佳方法是什么

1 个答案:

答案 0 :(得分:2)

我查看了您提供的演示链接:http://phpio.net/s/2bsw并且您的JSON字符串存在连接问题。它应该类似于以下内容然后它将打印数组结果:

$ele = '[{"element":"<div class=\'fb-autocomplete form-group field-autocomplete-1482728916013-preview\'><label for=\'autocomplete-1482728916013-preview\' class=\'fb-autocomplete-label\'>Autocomplete  </label> <input type=\'autocomplete\' class=\'autocomplete\' name=\'autocomplete-1482728916013-preview\' id=\'autocomplete-1482728916013-preview\'></div>"},{"element":"<div class=\'fb-button form-group field-button-1482728916826-preview\'><button type=\'button\' class=\'button-input btn-default btn\' name=\'button-1482728916826-preview\' style=\'default\' id=\'button-1482728916826-preview\'>Button</button></div>"},{"element":"<div class=\'fb-checkbox form-group field-checkbox-1482728917611-preview\'><input type=\'checkbox\' class=\'checkbox\' name=\'checkbox-1482728917611-preview\' id=\'checkbox-1482728917611-preview\'> <label for=\'checkbox-1482728917611-preview\' class=\'fb-checkbox-label\'>Checkbox  </label></div>"},{"element":"<div class=\'fb-date form-group field-date-1482728918827-preview\'><label for=\'date-1482728918827-preview\' class=\'fb-date-label\'>Date Field  </label> <input type=\'date\' class=\'calendar\' name=\'date-1482728918827-preview\' id=\'date-1482728918827-preview\'></div>"}]';

有关引用字符串连接的更多信息,请参阅以下链接:

  

http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single

希望这有帮助