开发者

json encode in php

开发者 https://www.devze.com 2023-01-30 11:33 出处:网络
I am storing this {background:\"default.jpg\"} in the database in field of tables as i am taking the table fields data and makint the json by json_encode while encoding this json also get encoded and

I am storing this {background:"default.jpg"} in the database in field of tables as i am taking the table fields data and makint the json by json_encode while encoding this json also get encoded and it is not valid json so how should i encode these json.

{"id_session":"c72b0581e7675b596a7651a7bb906438","gibid":"54","name":"Market Place","type":"S","num_owners":"0","inner_template":"","inner_data":"{background:\"default.jpg\"}","outer_template":"","o

it is adding the slashes how should i get ret开发者_JAVA技巧urn the valid json.

thanks.


  1. Don't store JSON in the database, store it in a neutral format, like key/value columns. Or:
  2. Decode the JSON, merge it into the array to be encoded, then encode it.

    $data = array('id' => ...);
    $data['inner_data'] = json_decode($databaseJson, true);
    json_encode($data);
    


So you have a JSON string in the database and then you get it out of it and do a json_ecnode on ir again? For php you are just encoding some string (thats why the backslashes on the quotations marks are coming from).

But to achieve what you actually want, you could to decode the JSON string from the database first and then encode it with the rest of your data again.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号