[英]jQuery load txt file with arrays and format as JSON
我正在尝试获取一个带有多维数组的.txt文件,并通过ajax加载该文件,并对数据进行排序并将其吐到我的网站上。 数据只是以纯文本形式返回,即使当我在其上使用JSON.parse()时也没有任何返回。
我正在使用它来访问文件:
$.get("json/json_data.txt", function(json) {
json = JSON.parse(json);
});
该文件如下所示:
array(array('industry' => 'Advertising/Media',array(
'no_hover' => 0,
'organization' => 'Marina Reef',
'existing_url => 'http://www.alphasoftware.com/marina-reef-case-study.asp',
'heading' => '<h3>Giant Touch Screen App</h3>',
'description' => 'Interactive brochure application running on a 46\" touch screen.',
'logo' =>'marina-reef-sized.pmg',
'large_image' => 'marina-reef-large.jpg',
'page_name' => 'marina'
)),
array('industry' => 'Construction/Engineering/Real Estate',array(
'no_hover' => 0,
'organization' => 'Al Reyami',
'existing_url => 'http://www.alphasoftware.com/al-reyami-case-study.asp',
'heading' => '<h3>Enterprise-wide System for Invoicing, Financial Management, Inventory, Human Resources, and More</h3>',
'description' => 'Global construction firm uses Alpha Anywhere as its enterprise development and deployment platform, because it required less code than other tools.',
'logo' =>'al-reyami-sized.png',
'large_image' => 'al-reyami-large.jpg',
'page_name' => ''
))
);
问题在于该文件不是JSON。 您必须编写一个自定义解析器,或将文件转换为有效的JSON。 问题是“数组”用于数组和对象。 您将必须完全操纵该结构。
此外,JSON需要在大多数情况下使用双引号,并且=>
所有实例都需要更改为:
...
TL; DR您需要结束此,而且这并不容易:
[{ "industry": "Advertising/Media", "no_hover": 0, "organization": "Marina Reef", "existing_url": "http://www.alphasoftware.com/marina-reef-case-study.asp", "heading": "<h3>Giant Touch Screen App</h3>", "description": "Interactive brochure application running on a 46\\" touch screen.", "logo": "marina-reef-sized.pmg", "large_image": "marina-reef-large.jpg", "page_name": "marina" }, { "industry": "Construction/Engineering/Real Estate", "no_hover": 0, "organization": "Al Reyami", "existing_url": "http://www.alphasoftware.com/al-reyami-case-study.asp", "heading": "<h3>Enterprise-wide System for Invoicing, Financial Management, Inventory, Human Resources, and More</h3>", "description": "Global construction firm uses Alpha Anywhere as its enterprise development and deployment platform, because it required less code than other tools.", "logo": "al-reyami-sized.png", "large_image": "al-reyami-large.jpg", "page_name": null }]
另外 ,您可能希望将'industry'值用作'ke y',表示每个相同的条目,例如: { "Advertising/Media": { "no_hover": 0, "organization": "Marina Reef", .... }, "Construction/Engineering/Real Estate": { "no_hover": 0, "organization": "Al Reyami", "existing_url": "http://www.alphasoftware.com/al-reyami-case-study.asp", .... } }
这两种方法都可以使用,但是取决于您以后打算如何访问JSON对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.