在 Javascript 中解析 PHP/JSON 数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10831758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 11:11:49  来源:igfitidea点击:

Parsing PHP/JSON data in Javascript

phpjavascriptajaxjson

提问by Sushil

I'm trying to communicate AJAX, JSON to PHP and then PHP returns some data and I'm trying to parse it with Javascrpt.

我正在尝试将 AJAX、JSON 与 PHP 通信,然后 PHP 返回一些数据,我正在尝试使用 Javascrpt 解析它。

From the php, server I return,

从 php,我返回的服务器,

    echo json_encode($data); 

    // it outputs ["123","something","and more something"]

and then in client-side,

然后在客户端,

success : function(data){

    //I want the data as following

    // data[0] = 123
    // data[1] = something
    // data[3] = and more something
}

But, it gives as;

但是,它给出了;

        data[0] = [ 
        data[1] = " 
        data[2] = 1

It is reading each character but I want strings from the array, not individual characters. What is happening here? Thanks in advance, I am new to Javascript and JSON, AJAX.

它正在读取每个字符,但我想要数组中的字符串,而不是单个字符。这里发生了什么?提前致谢,我是 Javascript 和 JSON、AJAX 的新手。

回答by Christoph Grimmer-Dietrich

JSON.parse(data)should do the trick.

JSON.parse(data)应该做的伎俩。

回答by Kabilan S

Check this one... Should Work

检查这个...应该工作

success : function(data){

成功:功能(数据){

var result = data;

result=result.replace("[","");

result=result.replace("]","");

var arr = new Array();

arr=result.split(",")

alert(arr[0]); //123

alert(arr[1]); //something

alert(arr[2]); //......
}

回答by VisioN

Set the dataTypeproperty of the ajax call to json. Then jQuery will automatically convert your response to object representation.

dataTypeajax 调用的属性设置为json. 然后 jQuery 会自动将您的响应转换为对象表示。

$.ajax({
    url : ...,
    data : ...,
    dataType : "json",
    success : function(json) {
        console.log(json);
    }
});

Another option is to set headers in PHP so that JQuery understand that you send a JSON object.

另一种选择是在 PHP 中设置标头,以便 JQuery 了解您发送的是 JSON 对象。

header("Content-Type: application/json");
echo json_encode($data);

回答by qfel13

You did not shown function in which you parse data. But you shoud use

您没有显示解析数据的函数。但你应该使用

JSON.parse

and if broser does not support JSON then use json polyfill from https://github.com/douglascrockford/JSON-js

如果 broser 不支持 JSON,则使用https://github.com/douglascrockford/JSON-js 中的json polyfill

dataArray = JSON.parse(dataFomXHR);

回答by Emmanuel Okeke

I'm not sure if this is what you want but why don't you want php to return it in this format:

我不确定这是否是您想要的,但您为什么不希望 php 以这种格式返回它:

{'item1':'123','item2':'something','item3':'and more something'}

Well to achieve this, you'll need to make sure the array you json_encode()is associative. It should be in the form below

那么要实现这一点,您需要确保您的数组json_encode()是关联的。它应该是下面的表格

array("item1"=>123,"item2"=>"something","item3"=>"more something");

You could even go ahead to do a stripslashes()in the event that some of the values in the array could be URLs

如果stripslashes()数组中的某些值可能是 URL,您甚至可以继续执行

You could then do a JSON.parse()on the JSON string and access the values

然后您可以JSON.parse()对 JSON 字符串执行 a并访问值

Hop this helps!

跳这有帮助!