뒤에 코드에서 얻은이 문자열이 있습니다.
{Name:"Tshirt", CatGroupName:"Clothes", Gender:"male-female"}, {Name:"Dress", CatGroupName:"Clothes", Gender:"female"}, {Name:"Belt", CatGroupName:"Leather", Gender:"child"}
다음과 같은 객체 배열로 변환해야합니다.
var Categories =
[
{Name:"Tshirt", CatGroupName:"Clothes", Gender:"male-female"},
{Name:"Dress", CatGroupName:"Clothes", Gender:"female"},
{Name:"Belt", CatGroupName:"Leather", Gender:"child"}
];
몇 가지 기능을 수행해야하기 때문입니다. ( $.grep
등등)
어떻게 변환 할 수 있습니까? 미리 고맙습니다.
가장 좋은 해결책은 백엔드가 적절한 JSON을 반환하도록하는 것이며 클라이언트에서 처리하기 위해 특별한 작업을 수행 할 필요가 없습니다. 유효한 JSON을 만들려면 키를 큰 따옴표로 묶고 전체 문자열을 대괄호로 묶어 배열로 만들어야합니다.
그러나 백엔드를 만질 수 없다면 새로운 기능을 사용하여 변환 할 수 있습니다.
var str = '{Name:"Tshirt", CatGroupName:"Clothes", Gender:"male-female"}, {Name:"Dress", CatGroupName:"Clothes", Gender:"female"}, {Name:"Belt", CatGroupName:"Leather", Gender:"child"}';
var obj = (new Function("return [" + str + "];")());
console.log(obj);
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다