我有一个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值?
答案 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"]);