gpt4 book ai didi

php - JSON 格式化

转载 作者:行者123 更新时间:2023-11-28 10:09:05 25 4
gpt4 key购买 nike

使用发现的方法here ,我正在收集表单值并使用以下代码发布它们:

$.ajax({
type: "POST",
url: "http://"+document.domain+"/SimplaAdmin/includes/rpc.php",
data: { data:postdata, method: 'addSite'},
dataType: "json",
.......

发布的数据是:

data:{
"textfield": ["",""],
"dropdown": ["option1","option1"],
"siteTitle":"this is the site title",
"siteKey":"",
"siteurl":"",
"address1":"",
"address2":"",
"address3":"",
"landline":"",
"method":"addSite",
"small-input":"",
"medium-input":"",
"large-input":""
}

然后我尝试使用以下方法获取值 siteTitle:

$data = $_POST['data'];
$obj=json_decode($data) ;
$title = $obj->{'siteTitle'};

但是不行,我的想法哪里出了问题?

最佳答案

您的语法不正确 - 您只需要 $title = $obj->siteTitle;

此外,我认为您出于本文的目的,在 JSON 字符串的开头添加了 data: ?那也不应该在那里。

举个例子:

<?php 

$string = '{"textfield":["",""],"dropdown":["option1","option1"],"siteTitle":"this is the site title","siteKey":"","siteurl":"","address1":"","address2":"","address3":"","landline":"","method":"addSite","small-input":"","medium-input":"","large-input":""}';

$obj = json_decode($string);

print_r( $obj->siteTitle );

?>

哪些输出

这是网站标题

关于php - JSON 格式化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7748032/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com