Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JSON和PHP编码乘法数组_Javascript_Html_Json_Parsing_Encode - Fatal编程技术网

Javascript JSON和PHP编码乘法数组

Javascript JSON和PHP编码乘法数组,javascript,html,json,parsing,encode,Javascript,Html,Json,Parsing,Encode,在我的PHP脚本中,我试图将我的multiply news数组编码为JSON 我的新闻阵列: $news[1]["header"] = "First header"; $news[1]["content"] = "First news content"; $news[2]["header"] = "Second header"; $news[2]["content"] = "Second news content"; 我使用此函数对该数组进行编码: json_encode($news);

在我的PHP脚本中,我试图将我的multiply news数组编码为JSON

我的新闻阵列:

$news[1]["header"] = "First header";
$news[1]["content"] = "First news content";

$news[2]["header"] = "Second header";
$news[2]["content"] = "Second news content";
我使用此函数对该数组进行编码:

json_encode($news);
之后,我在JS脚本中发送这个JSON数据

var result = JSON.parse(xmlHttp.responseText); // Here is my JSON object
例如,如何获取JavaScript代码中的第一个新闻标题

谢谢你的帮助。我很难理解json语法。

json.parse创建了一个属性键为1、2等的对象。因此,您可以使用结果[1]访问第一个标头。标头或结果[1]['header']

您还可以通过迭代结果来访问标题:

for (var index in result) {
    if (result.hasOwnProperty(index)) {
        console.log(result[index].header);
    }
}
这将产生:

First header
Second header
编辑:这里是一个演示: var json='{1:{header:First header,content:First news content},2:{header:Second header,content:Second news content}'; var result=JSON.parsejson; console.logresult[1]。标头; 对于结果中的var指数{ 如果result.hasOwnPropertyindex{ console.logresult[index]。标头; } } var json='{1:{header:First header,content:First news content},2:{header:Second header,content:Second news content}'; var result=JSON.parsejson; console.logresult[1]。标头; 对于结果中的var指数{ 如果result.hasOwnPropertyindex{ console.logresult[index]。标头; }
}结果[1]。标头返回Nothing结果和xmlHttp.responseText的值是多少?在我的答案中添加了演示脚本。非常奇怪。你的演示工作是正确的。根据我的回答,文本不返回任何内容。我只是没有在php脚本中编写json_编码。现在它工作正常了。多谢各位!请补充一些解释。