使用Prototype将字符串转换为JSON数组?

时间:2012-07-25 15:17:20

标签: javascript prototypejs

我有这个json字符串:

{"17":{"31":{"price":5,"oldPrice":5,"priceValue":"5.0000","type":"fixed","excludeTax":5,"includeTax":5},"29":{"price":8,"oldPrice":8,"priceValue":"8.0000","type":"fixed","excludeTax":8,"includeTax":8},"30":{"price":10,"oldPrice":10,"priceValue":"10.0000","type":"fixed","excludeTax":10,"includeTax":10}},"12":{"price":0,"oldPrice":0,"priceValue":"0.0000","type":"fixed","excludeTax":0,"includeTax":0},"11":{"price":0,"oldPrice":0,"priceValue":"0.0000","type":"fixed","excludeTax":0,"includeTax":0}}

如何使用Prototype将其转换为数组?我尝试了evalJSON(),但它没有用。

2 个答案:

答案 0 :(得分:0)

内置JSON.parse应该这样做,你不需要任何库:

var obj = JSON.parse(jsonString);

答案 1 :(得分:0)

您的字符串不是有效的JSON,因为它包含两个星号。删除它们并在之后应用evalJSON:

str.substring(1, -1).evalJSON(true);

虽然我会说你应该使用JSON.parse(或像json2这样的polyfill)而不是"".evalJSON

请注意,您将获得一个对象,而不是一个数组!