嵌入在json字符串中的日期 [英] Date embedded in json string
问题描述
我有一个函数,该函数接收一个String并将其转换为json对象.
I've a function, that receives a String and convert It to a json Object.
问题是,有时te函数可以获取一个Date,而我需要将它作为一个date! (不是字符串).
The problem is, that sometimes, te function could recibe a Date, and I need it to be a date! (Not a string).
例如,我发送给函数的字符串是这样的:
For example, the string that I send to the function is like this:
[{
"date" : new Date(2013 - 01 - 01),
"Weigth" : "120.00"
}, {
"date" : new Date(2013 - 01 - 01),
"Weigth" : "110.00"
}
]
在其他情况下,字符串将如下所示:
In other cases, the string will look like this:
[{
"age" : 12,
"Weigth" : "120.00"
}, {
"age" : 15,
"Weigth" : "110.00"
}
]
但是,在第一个示例中,解析器将引发错误. 如何检测到这种情况,并强制解析器生成带有此属性的javascript对象?
But, in the first example, the parser throws an error. How can I detect this case, and force the parcer to generate an javascript object with this atributes?
谢谢!
推荐答案
最简单的方法是eval()
您的字符串,但是我建议您生成一个正确的JSON
,因为eval()
可能会导致以下问题:不值得通过这种简单的转换来获取利润:
The easiest way is to eval()
your string but I would suggest you to generate a correct JSON
instead because eval()
may lead to the problems that doesn't worth the profit of this easy conversion:
var invJSIN = '[{"date":new Date(2013-01-01),"Weigth":"120.00"},{"date":new Date(2013-01-01),"Weigth":"110.00"}]'
var something = eval(invJSON)
这篇关于嵌入在json字符串中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!