如何在Angular 4中将JsonArray字符串转换为JsonArray?

时间:2018-03-13 05:32:43

标签: json angular

"[{\"key\":\"432100000000000300BB\",\"payLoad\":{\"payLoad\":{\"firstName\":\"postop\",\"lastName\":\"postop\",\"gender\":\"null\",\"patId\":\"432100000000000300BB\",\"anesthesiologist\":\"[]\",\"bedId\":\"1KY0012\",\"bedName\":\"PACU bed 1-1-1\",\"roomName\":\"Recovery 1\",\"roomId\":\"100000000001U61J3U\",\"key\":\"432100000000000300BZ\"},\"siteId\":2,\"id\":\"432100000000000300BB\"}},{\"key\":\"4321000000000003004J\",\"payLoad\":{\"payLoad\":{\"firstName\":\"pat1b\",\"lastName\":\"pat1b\",\"patId\":\"4321000000000003004J\",\"anesthesiologist\":\"[]\",\"bedId\":\"null\",\"bedName\":\"---\",\"roomName\":\"---\",\"roomId\":\"null\",\"key\":\"43210000000000030057\"},\"siteId\":2,\"id\":\"4321000000000003004J\"}},{\"key\":\"4321000000000003000J\",\"payLoad\":{\"payLoad\":{\"firstName\":\"pat1b\",\"lastName\":\"pat1b\",\"patId\":\"4321000000000003000J\",\"anesthesiologist\":\"[]\",\"bedId\":\"null\",\"bedName\":\"---\",\"roomName\":\"---\",\"roomId\":\"null\",\"key\":\"4321000000000003001C\"},\"siteId\":2,\"id\":\"4321000000000003000J\"}}]"

上面的JsonArray字符串必须转换为JsonArray以下。

[{"payLoad":{"firstName":"postop","lastName":"postop","gender":"null","patId":"432100000000000300BB","anesthesiologist":"[]","bedId":"1KY0012","bedName":"PACU bed 1-1-1","roomName":"Recovery 1","roomId":"100000000001U61J3U","key":"432100000000000300BZ"},"siteId":2,"id":"432100000000000300BB"},{"payLoad":{"firstName":"pat1b","lastName":"pat1b","patId":"4321000000000003004J","anesthesiologist":"[]","bedId":"null","bedName":"---","roomName":"---","roomId":"null","key":"43210000000000030057"},"siteId":2,"id":"4321000000000003004J"},{"payLoad":{"firstName":"pat1b","lastName":"pat1b","patId":"4321000000000003000J","anesthesiologist":"[]","bedId":"null","bedName":"---","roomName":"---","roomId":"null","key":"4321000000000003001C"},"siteId":2,"id":"4321000000000003000J"}]

我尝试过以下代码但无法转换它。

console.log("my data "+JSON.parse(this.getResult));

this.obj = JSON.stringify(this.getResult);

console.log("Object "+this.obj);

1 个答案:

答案 0 :(得分:0)

这是你的解决方案

result = "[{\"key\":\"432100000000000300BB\",\"payLoad\":{\"payLoad\":{\"firstName\":\"postop\",\"lastName\":\"postop\",\"gender\":\"null\",\"patId\":\"432100000000000300BB\",\"anesthesiologist\":\"[]\",\"bedId\":\"1KY0012\",\"bedName\":\"PACU bed 1-1-1\",\"roomName\":\"Recovery 1\",\"roomId\":\"100000000001U61J3U\",\"key\":\"432100000000000300BZ\"},\"siteId\":2,\"id\":\"432100000000000300BB\"}},{\"key\":\"4321000000000003004J\",\"payLoad\":{\"payLoad\":{\"firstName\":\"pat1b\",\"lastName\":\"pat1b\",\"patId\":\"4321000000000003004J\",\"anesthesiologist\":\"[]\",\"bedId\":\"null\",\"bedName\":\"---\",\"roomName\":\"---\",\"roomId\":\"null\",\"key\":\"43210000000000030057\"},\"siteId\":2,\"id\":\"4321000000000003004J\"}},{\"key\":\"4321000000000003000J\",\"payLoad\":{\"payLoad\":{\"firstName\":\"pat1b\",\"lastName\":\"pat1b\",\"patId\":\"4321000000000003000J\",\"anesthesiologist\":\"[]\",\"bedId\":\"null\",\"bedName\":\"---\",\"roomName\":\"---\",\"roomId\":\"null\",\"key\":\"4321000000000003001C\"},\"siteId\":2,\"id\":\"4321000000000003000J\"}}]"
console.log("my data "+JSON.stringify(result));
$scope.stringfu = JSON.stringify(result);
$scope.main = JSON.parse($scope.stringfu);
console.log($scope.main);

你需要首先进行字符串化,然后进行解析。对于我的解释,我使用angularjs你可以类似地在打字稿中使用这个代码。