这是将JSON解析为var的正确方法吗?

时间:2015-05-18 07:34:57

标签: javascript json

我有这个carData JSON: 这对于解析JSON数据是否正确?

var carData = '[{"ConditionNotes":{},"Photos":{},"RealChassisNo":"BJ5W-315859","Age":0,"Miles":0,"ImprovementCost":0,"AirBagType":0,"M3":0,"Category":0,"IsMiles":false,"IsInnerCargo":false,"IsPreSold":false,"IsNoNZ":false,"InnerCargoNote":null,"DateStock":"\/Date(-62135596800000)\/","IsSold":false,"PriceSales":0,"PriceConfirmation":0,"PriceExport":0,"PriceATSales":0,"ScaledMargin":0,"PriceDynamic":0,"FobFees":332000.0000,"OFSFees":1440.0000,"CIFFees":0,"FreightRate":0,"PriceBase":0,"MarketTerm":null,"IsIncludeOPC":false,"CustomerTerm":null,"LandedCost":0,"NZNote":null,"NZStructuralNote":null,"NZStructuralCost":0,"IDIBCInfo":null,"TransportStatus":null,"DateDeparture":"\/Date(1281801600000)\/","DateArrive":"\/Date(1283788800000)\/","ShipName":"Saracen Star(KB)","TypeOfSale":"","BuyTripCustomer":"","IsSplitCurrency":false,"Currency":"JPY","CountryID":75,"IsFollowing":false,"FrieghtAndInsuranceTerm":null,"FrieghtAndInsuranceFees":0,"PriceBaseForATNZ":0,"IDVehicle":"232960","Price":0,"MaskChassisNo":null,"Make":"MAZDA","Model":"FAMILIA","Year":2002,"Month":0,"Type":null,"Location":null,"Grade":"S-F","Transmission":null,"KMS":0,"EngineDisplacement":null,"Fuel":null,"Doors":null,"ConditionGrade":null,"Color":null,"IsPowerSteering":false,"IsPowerWindow":false,"IsAirConditioned":false,"InteriorColor":null,"InteriorType":null,"HasSunRoof":false,"Length":0,"Width":0,"Height":0,"WheelStyle":0,"HasSpareKey":false,"SalesNote":null,"IsSale":false,"IsNewArrival":false,"IsReserved":false,"IsThailandStocks":false,"IsLeftHandDrive":false,"DateInspected":"\/Date(-62135596800000)\/","ExteriorVideo":null,"InteriorVideo":null,"IsSingaporeStocks":false,"IsUKStocks":false,"IsOriginalLHD":false,"CustomerPort":null,"Airbag":0,"IsNonSmoker":false,"Tonnage":0,"InteriorGrade":null,"DateConfirm":"\/Date(-62135596800000)\/"}]';
var obj = JSON.parse(carData);

4 个答案:

答案 0 :(得分:1)

是的,你在javascript中编写了正确的json解析语句。 更多信息,我发布了另一个javascript json解析的例子。

var text = '{ "employees" : [' +
'{ "firstName":"John" , "lastName":"Doe" },' +
'{ "firstName":"Anna" , "lastName":"Smith" },' +
'{ "firstName":"Peter" , "lastName":"Jones" } ]}';

var obj = JSON.parse(text);

答案 1 :(得分:1)

是的,这是正确的方法。 mesa会将您的JSON.parse()字符串转换为JavaScript对象。

可以找到更多详细信息here

答案 2 :(得分:1)

是的,这是正确的方式, 但是如果你使用旧浏览器(不支持JSON.parse函数)你应该使用eval()

eval ("(" + carData + ")")

答案 3 :(得分:1)

这将创建一个只有1个元素的JSON数组。如果你有一个元素,为什么不删除结尾处的'['和']'。 无论如何,JSON.parse(carData)将成功解析您的JSON。 obj[0]将获得此JSON的详细信息