如何Json解析包含" @"?的字符串?

时间:2015-06-22 21:07:08

标签: javascript json parsing

我有一个json对象,如下所示:

{\"household\":{\"@id\":\"36963416\",\"@uri\":\"https://serviceapi.com/v1/Households/36963416\",\"@oldID\":\"\",\"@hCode\":\"8Z6iDWwZ7Rr3uY/atTYRMQ==\",\"householdName\":\"Test Test\",\"householdSortName\":\"Test\",\"householdFirstName\":\"Test\",\"lastSecurityAuthorization\":null,\"lastActivityDate\":null,\"createdDate\":\"2015-06-22T15:53:31\",\"lastUpdatedDate\":null}}

我在使用JSON.parse()时遇到困难,因为讨厌的" @"迹象。我应该如何解析这个以便获得家庭@id值?

2 个答案:

答案 0 :(得分:3)

使用此JavaScript表达式访问家庭id

var object= JSON.parse(yourString);
object.household["@id"]

具体是:

var obj= JSON.parse("{\"household\":{\"@id\":\"36963416\",\"@uri\":\"https://serviceapi.com/v1/Households/36963416\",\"@oldID\":\"\",\"@hCode\":\"8Z6iDWwZ7Rr3uY/atTYRMQ==\",\"householdName\":\"Test Test\",\"householdSortName\":\"Test\",\"householdFirstName\":\"Test\",\"lastSecurityAuthorization\":null,\"lastActivityDate\":null,\"createdDate\":\"2015-06-22T15:53:31\",\"lastUpdatedDate\":null}}");
console.log('id:', obj.household['@id']);

答案 1 :(得分:2)

您可以正常解析JSON:

var test = "{\"household\":{\"@id\":\"36963416\",\"@uri\":\"https://serviceapi.com/v1/Households/36963416\",\"@oldID\":\"\",\"@hCode\":\"8Z6iDWwZ7Rr3uY/atTYRMQ==\",\"householdName\":\"Test Test\",\"householdSortName\":\"Test\",\"householdFirstName\":\"Test\",\"lastSecurityAuthorization\":null,\"lastActivityDate\":null,\"createdDate\":\"2015-06-22T15:53:31\",\"lastUpdatedDate\":null}}";
var objTest = JSON.parse(test);
console.log(objTest);
console.log(objTest["@id"]);

Working example