根据http://www.w3schools.com/json/json_syntax.asp,JSON"数组"如下所示:
"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter","lastName":"Jones"}
]
根据同一来源,这是一个JSON"对象":
{"firstName":"John", "lastName":"Doe"}
我已经为我的应用程序开发了一个API,这是我收到的JSON响应:
{
"status":"success",
"message": [
{
"entry_id":"1",
"entry_name":"12345678",
"entry_body":"this is just the beginning update",
"entry_date":"2016-05-01 08:25:51",
"status":"active","created_timestamp":"2016-05-01 21:25:51",
"updated_timestamp":null
},
{
"entry_id":"2",
"entry_name":"one one one",
"entry_body":"this is just the beginning update 1",
"entry_date":"2016-05-02 01:44:03",
"status":"active",
"created_timestamp":"2016-05-02 14:44:03",
"updated_timestamp":null
},
{
"entry_id":"3",
"entry_name":"two two two",
"entry_body":"this is just the beginning update 2",
"entry_date":"2016-05-02 01:44:13",
"status":"active",
"created_timestamp":"2016-05-02 14:44:13",
"updated_timestamp":null
}
]
}
在数据表中,我制作了以下脚本,但它没有读取JSON文件:
var table = $('#list_blogs_table').DataTable({
"ajax": {
"url": "/BlogApiV1/BlogApi/blogs",
"dataSrc": "message",
}
"columnDefs":
[
{
我的URL必须是一个GET方法(我这样做正确吗)?
另外,假设我的JSON数据被认为是ARRAY。这是正确的吗?我通过声明"dataSrc": "message",
告诉数据表,数据从"message"开始。这是正确的吗?
您的JSON包含多个隐藏的非法控制字符,这些字符似乎是转义符和无效制表符。即使您的JSON可以作为本地定义的文字来工作,但当通过网络传递并在客户端上进行JSONified时,它也会失败。
将来,您可以使用https://jsonformatter.curiousconcept.com查看JSON到底出了什么问题。您可以使用https://myjson.com也要验证,因为它使用的是JSON.parse()
,而不是像那样的算法测试http://jsonlint.com这会错误地认为您的JSON是有效的。
因此,只需清理JSON,它就可以工作:
var table = $('#example').DataTable({
ajax: {
url: "https://api.myjson.com/bins/14lrs",
dataSrc: "message",
},
columnDefs: [
{ targets: 0, data: 'entry_id' },
{ targets: 1, data: 'entry_name' }
]
})
演示->http://jsfiddle.net/3wr2j2yx/
尝试从中获取已清理的JSONhttp://myjson.com/14lrs并将其用作源。
我的URL必须是一个GET方法(我这样做正确吗)?
是的!
另外,假设我的JSON数据被认为是ARRAY。这是正确的吗?我通过声明告诉datatables数据从"message"开始"dataSrc":"消息",。这是正确的吗?
是的!
要查看json变成了什么,请执行var_dump或var_export。
假设您的响应存储在变量$response
中
$object = json_decode($response);
var_export($object);
var_dump($object);
我们得到以下信息:
stdClass::__set_state(array(
'status' => 'success',
'message' =>
array (
0 =>
stdClass::__set_state(array(
'entry_id' => '1',
'entry_name' => '12345678',
'entry_body' => 'this is just the beginning update',
'entry_date' => '2016-05-01 08:25:51',
'status' => 'active',
'created_timestamp' => '2016-05-01 21:25:51',
'updated_timestamp' => NULL,
)),
1 =>
stdClass::__set_state(array(
'entry_id' => '2',
'entry_name' => 'one one one',
'entry_body' => 'this is just the beginning update 1',
'entry_date' => '2016-05-02 01:44:03',
'status' => 'active',
'created_timestamp' => '2016-05-02 14:44:03',
'updated_timestamp' => NULL,
)),
2 =>
stdClass::__set_state(array(
'entry_id' => '3',
'entry_name' => 'two two two',
'entry_body' => 'this is just the beginning update 2',
'entry_date' => '2016-05-02 01:44:13',
'status' => 'active',
'created_timestamp' => '2016-05-02 14:44:13',
'updated_timestamp' => NULL,
)),
),
))
object(stdClass)#511 (2) {
["status"]=>
string(7) "success"
["message"]=>
array(3) {
[0]=>
object(stdClass)#512 (7) {
["entry_id"]=>
string(1) "1"
["entry_name"]=>
string(8) "12345678"
["entry_body"]=>
string(33) "this is just the beginning update"
["entry_date"]=>
string(19) "2016-05-01 08:25:51"
["status"]=>
string(6) "active"
["created_timestamp"]=>
string(19) "2016-05-01 21:25:51"
["updated_timestamp"]=>
NULL
}
[1]=>
object(stdClass)#513 (7) {
["entry_id"]=>
string(1) "2"
["entry_name"]=>
string(11) "one one one"
["entry_body"]=>
string(35) "this is just the beginning update 1"
["entry_date"]=>
string(19) "2016-05-02 01:44:03"
["status"]=>
string(6) "active"
["created_timestamp"]=>
string(19) "2016-05-02 14:44:03"
["updated_timestamp"]=>
NULL
}
[2]=>
object(stdClass)#514 (7) {
["entry_id"]=>
string(1) "3"
["entry_name"]=>
string(11) "two two two"
["entry_body"]=>
string(35) "this is just the beginning update 2"
["entry_date"]=>
string(19) "2016-05-02 01:44:13"
["status"]=>
string(6) "active"
["created_timestamp"]=>
string(19) "2016-05-02 14:44:13"
["updated_timestamp"]=>
NULL
}
}
}
正如你所看到的,你的结构解析为一个对象,这是一个包含一个数组的关联数组,该数组包含一个作为关联数组的对象;
这意味着您不能作为关联数组进行访问。
所以$object〔'message'〕不会工作,但$object->消息会工作:-)
var_dump($var->message[0])
解析为
object(stdClass)#512 (7) {
["entry_id"]=>
string(1) "1"
["entry_name"]=>
string(8) "12345678"
["entry_body"]=>
string(33) "this is just the beginning update"
["entry_date"]=>
string(19) "2016-05-01 08:25:51"
["status"]=>
string(6) "active"
["created_timestamp"]=>
string(19) "2016-05-01 21:25:51"
["updated_timestamp"]=>
NULL
}
你可以这样访问它:-)
$myobj = $var->message[0]
echo $myobj->status == 'active'
在javascript中,它应该是
var obj = JSON.parse(message);
myobj = obj.message[0];
echo myobj.status == 'active';
如果您覆盖jquery的ajax调用,您可以看到下面的调用被发送到您的服务器(请参阅我的代码片段中的代码,了解如何查看)
正如您所看到的,您的参数会作为额外的参数发送到服务器。
[对象]0:对象缓存:false数据:对象dataSrc:"消息"dataType:"json"错误:(b,c)成功:(b)类型:"GET"url:"/BloggApiV1/BloggAPi/blogs"
您的ajax响应应该是这样的:
{
"status":"success",
"data": [
[
"1",
"12345678",
"this is just the beginning update",
"2016-05-01 08:25:51",
"active",
"2016-05-01 21:25:51",
null
],
[
"2",
"one one one",
"this is just the beginning update 1",
"2016-05-02 01:44:03",
"active",
"2016-05-02 14:44:03",
null
],
[
"3",
"two two two",
"this is just the beginning update 2",
"2016-05-02 01:44:13",
"active",
"2016-05-02 14:44:13",
null
]
]
}
请参阅数据表示例页面,并查看底部的源选项卡:-)他们给出了如何格式化内容的示例:-)
已经解决了问题。解决方案是根据@Michael Dibbets的大崩溃修复JSON输出。实际上,我正在使用Phil Sturgeon的REST服务器来帮助创建API。默认值似乎是JSON输出,然而,正如Michael Dibbets所指出的,JSON是无效的。REST服务器允许格式,包括xml、csv等。我没有依赖默认格式,而是在URL中特别说明了格式,比如/BloggApiV1/BloggCapi/blogs/format/json,这修复了一切。感谢大家的评论,当然也从这个错误中学到了很多。