0

I have a json file

NAME:abcd.json

Its content starts like following(this looked pretty wierd to me as this was the first time i came across such json file)-->

["51b59c2081ecf", [["parties", 0.0793, 0.238], ["signature", 0.002, 0.006]]]
["51b59c2683e7d", [["parties", 0.0037, 0.011]]]
["51b59c255e1fb", [["parties", 0.0037, 0.011]]]
["51b59c2e95da5", [["parties", 0.0037, 0.011]]]

I came across another json file

NAME:color.json

{
    "colorsArray":[{
            "colorName":"red",
            "hexValue":"#f00"
        },
        {
            "colorName":"green",
            "hexValue":"#0f0"
        },
        {
            "colorName":"blue",
            "hexValue":"#00f"
        },
        {
            "colorName":"black",
            "hexValue":"#000"
        }
    ]
}

I am using D3js and I want to push the content of abcd.json in to an array so that I can use that data further.

MyQuestions:

  1. if both of them are json.files than why there exist some difference between their syntax.My entire d3 code depends on this data.

2.How do I push the json data to a variable say Var data.

currently I am providing var data some static value for some temporary testing,Like this->

 var data= [
        ["51b59c2a019af", [["signature", 0.002, 0.006], ["parties", 0.0037, 0.011]]],
        ["51b59c2635997", [["fashion", 0.002, 0.006], ["parties", 0.0037, 0.011],["royal-challenge", 0.002, 0.6]]],
        ["51b59c29c39bf", [["parties", 0.0037, 0.1], ["signature", 0.002, 0.006]]]
           ];
4

2 回答 2

0

我认为前一个不是正确的 JSON,但是浏览器中的大多数库和 JSON.parse 仍然会解析它并返回一个数组。

请参见下面的 jQuery 代码:

parseJSON: function( data ) {
    // Attempt to parse using the native JSON parser first
    if ( window.JSON && window.JSON.parse ) {
        return window.JSON.parse( data );
    }

    if ( data === null ) {
        return data;
    }

    if ( typeof data === "string" ) {

        // Make sure leading/trailing whitespace is removed (IE can't handle it)
        data = jQuery.trim( data );

        if ( data ) {
            // Make sure the incoming data is actual JSON
            // Logic borrowed from http://json.org/json2.js
            if ( rvalidchars.test( data.replace( rvalidescape, "@" )
                .replace( rvalidtokens, "]" )
                .replace( rvalidbraces, "")) ) {

                return ( new Function( "return " + data ) )();
            }
        }
    }

    jQuery.error( "Invalid JSON: " + data );
}

它用于new Function()创建对象,类似于eval()(但更好,因为它无法访问代码的范围),因此在这种情况下它仍然会解析它。

于 2013-06-28T09:14:15.043 回答
0

[]如果您将文件内容保存在字符串中,则可以通过将其包装在内部并添加适当的逗号轻松地将其转换为有效的 JSON ;

var str = '["51b59c2081ecf", [["parties", 0.0793, 0.238], ["signature", 0.002, 0.006]]]' + "\n" + 
          '["51b59c2683e7d", [["parties", 0.0037, 0.011]]]' + "\n" +
          '["51b59c255e1fb", [["parties", 0.0037, 0.011]]]' + "\n" +
          '["51b59c2e95da5", [["parties", 0.0037, 0.011]]]' + "\n";

var valid_json = '[' + str.split(/[\n\r]/).
                           filter(function(val) { return (val !== ''); }).
                           join(',') + ']';

console.log(valid_json);
于 2013-06-28T09:23:38.260 回答