简体   繁体   English

使用JavaScript解析JSON对象数组

[英]Parse Array of JSON Objects using JavaScript

Having trouble parsing an array of JSON objects using JavaScript. 使用JavaScript解析JSON对象数组时遇到问题。 My PHP file gets database info such as this: 我的PHP文件获取如下数据库信息:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => holly
            [text] => Text 1
        )
    [1] => Array
        (
            [id] => 2
            [name] => Becky
            [text] => Text 2
        )
)

which I then run it through json_encode( to get this: 然后我通过json_encode(来运行它:

"[\r\n   {\r\n      \"id\":\"1\",\r\n      \"name\":\"holly\",\r\n          
\"text\":\"Text 1\"\r\n   },\r\n   {\r\n      \"id\":\"2\",\r\n      
\"name\":\"Becky\",\r\n      \"tet\":\"Text 2\"\r\n   }\r\n]"

This data gets called via a JavaScript function from here: http://oscargodson.com/posts/unmasking-jsonp.html 可以通过JavaScript函数从以下位置调用此数据: http : //oscargodson.com/posts/unmasking-jsonp.html

JSONP( url, function(json){console.log(json)});

The callback doesn't return anything I can parse, I just get [Object Object] if I do console.log(json). 回调不会返回我可以解析的任何内容,如果我执行console.log(json),则只会得到[Object Object]。 I am getting results from PHP because the Sources tab in Chrome's web developer shows the php file and the jsonified text. 我从PHP获得结果,因为Chrome的网络开发人员中的“来源”标签显示了php文件和jsonified文本。 I just can't figure out how to send it to JavaScript for parsing. 我只是不知道如何将其发送到JavaScript进行解析。

I've read tons of 'duplicate' questions and many say use JSON.stringify() but I don't get any results. 我读过很多“重复”的问题,很多人说使用JSON.stringify(),但是没有任何结果。

Is the error with my PHP or I'm just not using JavaScript properly? 是我的PHP错误还是我没有正确使用JavaScript? Please help. 请帮忙。

The JSONP() function from the article won't work with data from the database but will if the same data is hard-coded. 本文中的JSONP()函数不适用于数据库中的数据,但如果对相同的数据进行硬编码,则可以使用。 Weird. 奇怪的。

var result =  [
    {"id":"1","name":"holly","text":"Text 1"},    
    {"id":"2","name":"Becky","text":"Text 2"}
];

for (var i = 0; i < result.length; i++) {

}

weird. 奇怪的。 Not sure why that function doesn't return as expected but I guess that's why jQuery has $.getJSON() so we don't have to go through all of this every time. 不知道为什么该函数未按预期返回,但是我想这就是jQuery具有$ .getJSON()的原因,因此我们不必每次都经历所有这些。

You want to run your data through plain 您想通过纯数据运行数据

json_encode($data)

But please beware any other arguments passed to this call - because in the string that you posted (the result of the JSON encode function) - you had it pretty printed . 但是请注意传递给此调用的任何其他参数-因为在您发布的字符串(JSON编码函数的结果)中,您已经漂亮地打印了它。 Which is probably the main reason your JSONP calls to such resource fail - the data is pretty-printed - when it shouldn't be. 这可能是您的JSONP调用此类资源失败的主要原因-数据打印得很漂亮-不应该这样。

$.ajax(
{
    url: "callback/yourcallback.php", //at the end echo json_encode($array);
    dataType: "jsonp",
    data: { paramname: paramvalue},
    type: "POST"
})
.done(function( data )
{
    var response=$.parseJSON(data);
      console.log(response);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM