Javascript- 将文本框中的内容转换为键值对
Javascript- convert content in textbox to key value pair
我有一个文本框,它接受用户定义的键值对,例如:
{'Apple':'Red', 'Lemon':'Green'} 我希望将其转换为键值对数组。
我有代码:
var color= document.getElementById('txtColor').value;
问题是如果我尝试将它作为字符串获取:color['Apple']
它显示未定义;而我预计 'Red'。我如何进行转换才能得到类似的东西:
var color={'Apple':'Red', 'Lemon':'Green'}
并在 color['Apple']
上获取值 'Red'。
提前致谢。
我假设如下
let color = document.getElementById('txtColor').value; // This line returns {'Apple':'Red', 'Lemon':'Green'}
如果我是正确的,你可以执行以下操作
try {
// Here you can write logic for format json ex. Convert single quotes to double quotes
// This may help to convert well formatted json string
let colorJson = JSON.parse(color);
console.log(colorJson["Apple"]) // This will return your expected value.
} catch(e) {
console.log('Invalid json format')
}
我有一个类似的用例。您必须 JSON.parse()
值。
var obj = JSON.parse(document.getElementById('txtColor').value.replace(/'/g, '"'));
console.log(obj['Apple']);
<textarea id="txtColor">{'Apple':'Red', 'Lemon':'Green'}</textarea>
var color = document.getElementById('txtColor').value;
//JSON.stringify will validate the JSON string
var jsonValidString = JSON.stringify(eval("(" + color + ")"));
//If JSON string is valid then we can conver string to JSON object
var JSONObj = JSON.parse(jsonValidString);
//We can use JSON.KeyName or JSON["KeyName"] both way you can get value
console.log(JSONObj.Apple, " --- ", JSONObj["Apple"]);
console.log(JSONObj.Lemon, " --- ", JSONObj["Lemon"]);
<input id="txtColor" value="{'Apple':'Red', 'Lemon':'Green'}" type="text" />
我有一个文本框,它接受用户定义的键值对,例如: {'Apple':'Red', 'Lemon':'Green'} 我希望将其转换为键值对数组。 我有代码:
var color= document.getElementById('txtColor').value;
问题是如果我尝试将它作为字符串获取:color['Apple']
它显示未定义;而我预计 'Red'。我如何进行转换才能得到类似的东西:
var color={'Apple':'Red', 'Lemon':'Green'}
并在 color['Apple']
上获取值 'Red'。
提前致谢。
我假设如下
let color = document.getElementById('txtColor').value; // This line returns {'Apple':'Red', 'Lemon':'Green'}
如果我是正确的,你可以执行以下操作
try {
// Here you can write logic for format json ex. Convert single quotes to double quotes
// This may help to convert well formatted json string
let colorJson = JSON.parse(color);
console.log(colorJson["Apple"]) // This will return your expected value.
} catch(e) {
console.log('Invalid json format')
}
我有一个类似的用例。您必须 JSON.parse()
值。
var obj = JSON.parse(document.getElementById('txtColor').value.replace(/'/g, '"'));
console.log(obj['Apple']);
<textarea id="txtColor">{'Apple':'Red', 'Lemon':'Green'}</textarea>
var color = document.getElementById('txtColor').value;
//JSON.stringify will validate the JSON string
var jsonValidString = JSON.stringify(eval("(" + color + ")"));
//If JSON string is valid then we can conver string to JSON object
var JSONObj = JSON.parse(jsonValidString);
//We can use JSON.KeyName or JSON["KeyName"] both way you can get value
console.log(JSONObj.Apple, " --- ", JSONObj["Apple"]);
console.log(JSONObj.Lemon, " --- ", JSONObj["Lemon"]);
<input id="txtColor" value="{'Apple':'Red', 'Lemon':'Green'}" type="text" />