JSON转换问题

时间:2012-05-18 17:52:11

标签: javascript arrays json coldfusion

我正在尝试(在Javascript和Coldfusion中)转换:

{"val1":"member","val2":"book","val3":"journal","val4":"new_member","val5":"cds"},

进入这个:

{ member,book,journal,new_member,cds}

请注意,我正在尝试删除引号。

有可能实现这一目标吗?我该怎么办?

2 个答案:

答案 0 :(得分:2)

好的,所以这个: { “VAL1”: “成员”, “值2”: “书”, “VAL3”: “日志”, “VAL4”: “new_member”, “val5”: “CDS”} 是JSON。

要转换为CF结构,您可以这样:

myStruct = deserializeJSON('{"val1":"member","val2":"book","val3":"journal","val4":"new_member","val5":"cds"}');

(注意我的例子假设我们在<CFSCRIPT>块内运行。) 现在你有了一个带键/值对的简单结构。但是你想要一个值列表。所以让我们创建一个空字符串,然后将所有结构值附加到它:

myList = "";
for (k IN myStruct) {
  myList = listAppend(myList,myStruct[k]);
}

动臂。 myList现在应该是“member,book,journal,new_member,cds” 如果你真的想,请用花括号包裹它。

myList = "{"&myList&"}";

答案 1 :(得分:0)

首先,我要感谢你的回复。但是有些人必须对新手更有礼貌。

var tata = {"val1":"member","val2":"book","val3":"journal","val4":"new_member","val5":"cds"}
var arr=[]

for (var i in tata) {
    arr.push(tata[i])
}; 
console.log(arr);


wrd = new Array(arr)
var joinwrd = wrd.join(",");
console.log('{' + joinwrd + '}');