简体   繁体   English

如何在Javascript中解析/使用此JSON

[英]How to parse/use this JSON in Javascript

I have a variable with the following in my code: 我的代码中包含以下变量:

{
"Rows":
    [
        {
            "New":1,
            "CachedNumberType":0,
            "Date":1327479615921,
            "Type":2,
            "Number":"123456",
            "Duration":1
        }
    ]
}

I think it's JSON, how do I parse it? 我认为它是JSON,如何解析? (Eg, with json2.js?) Or how do I use it in my JavaScript? (例如,使用json2.js吗?)或者如何在JavaScript中使用它?

var jsonObj = JSON.parse(jsonString);

You've said that when you try JSON.parse on the variable containing the "JSON" that it says it can't parse it. 您已经说过,当对包含“ JSON”的变量尝试JSON.parse时,它说它无法解析它。 Could it be that it's already deserialized? 可能是它已经反序列化了吗? Or maybe it was never JSON at all? 还是根本就没有JSON? For instance, what you quoted, in JavaScript source, is an object literal containing an array literal containing another object literal; 例如,您在JavaScript源代码中引用的是一个对象常量,该对象常量包含一个包含另一个对象常量的数组常量; no JSON in sight. 看不到JSON。

If you do console.log(x.Rows[0].Date); 如果您执行console.log(x.Rows[0].Date); , where x is the variable you were trying to pass to JSON.parse, do you see the date value? ,其中x是您尝试传递给JSON.parse的变量,您看到日期值了吗?

A lot of people confuse JSON and JavaScript literal syntax, because JSON is a textual format derived from JavaScript literal syntax. 许多人将JSON和JavaScript文字语法弄混了,因为JSON是从JavaScript文字语法派生的文本格式。 I suspect that's what's happening here. 我怀疑这就是这里正在发生的事情。

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

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