jQuery加载txt文件,数组和格式为JSON

时间:2016-03-24 23:49:17

标签: javascript jquery arrays 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'  => ''      
    ))
);

1 个答案:

答案 0 :(得分:0)

问题是该文件不是JSON。您必须编写自定义解析器,或将文件转换为有效的JSON。问题是&#39;阵列&#39;用于数组和对象的目的;你必须完全操纵这个结构。

此外,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 }] ,您可能希望使用&#39;行业&#39;价值作为“关键”&#39;对于每个相同的条目,如下: { "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对象。