javascript json数组输出
具有JSON字符串(缩写): 我希望能够调用基于“短”值返回“长”值的函数: 比如:javascript json数组输出,javascript,json,Javascript,Json,具有JSON字符串(缩写): 我希望能够调用基于“短”值返回“长”值的函数: 比如: var test= 'Say '+get_value("_YES"); 我该怎么做 尝试: function f_lang(short_string) { var obj = json_string; var arr = []; json = JSON.stringify(eval('(' + obj + ')')); //convert to json string arr
var test= 'Say '+get_value("_YES");
我该怎么做
尝试:
function f_lang(short_string) {
var obj = json_string;
var arr = [];
json = JSON.stringify(eval('(' + obj + ')')); //convert to json string
arr = $.parseJSON(json); //convert to javascript array
return arr['line_array'][short_string];
}
如果运气不好,请使用查找包含短值的对象。请注意,IE不支持Array#find
。因此,如果您需要IE支持和/或正在进行大量类似的转换,您应该使用字典方法
var str='{“line_数组”:[{“short”:“[common]”,“long”:“undefined”},{“short”:“YES”,“long”:“YES”},{“short”:“NO”,“long”:“NO”},{“short”:“NOT”},{“short”:“NOT SEARCH”,“long”:“SEARCH”},{“short”:“GO”,“long”:“GO”};
var terms=JSON.parse(str);
函数get_值(短){
var term=terms.line\u array.find(函数(o){
返回o.short===short;
});
//如果找不到术语,我们将防止term.long抛出错误
返回期限(&term.long);
}
var结果=获取值(“是”);
控制台日志(结果)代码>通过正确解析的JSON,您可以使用ES6特性
函数getValue(短字符串){
return(object.line_数组.find(a=>a.short==short_字符串)|{}).long;
}
var json_string='{“line_array”:[{“short”:“[common],“long”:“undefined”},{“short”:“YES”,“long”:“YES”},{“short”:“NO”,“long”:“NO”},{“short”:“NOT”},{“short”:“NOT SEARCH”,“long”:“SEARCH”},{“short”:“GO”,“long”:“GO”},
object=JSON.parse(JSON_字符串),
测试='Say'+getValue('u YES');
控制台日志(测试)代码>另一个选项:
函数f_lang(短字符串){
var obj={“line_数组”:[{“short”:“[common],“long”:“undefined”},{“short”:“YES”,“long”:“YES”},{“short”:“NO”,“long”:“NO”},{“short”:“NOT”,“long”:“NOT”},{“short”:“SEARCH”,“long”:“SEARCH”},{“short”:“GO”,“long”:“GO”};
for(变量i=0;i您可以使用
请尝试下面的代码
<script>
var str = '{"line_array":[{"short":"[common]","long":"undefined"},{"short":"_YES","long":"Yes"},{"short":"_NO","long":"No"},{"short":"_NOT","long":"Not "},{"short":"_SEARCH","long":"Search"},{"short":"_GO","long":"Go"}]}';
var terms = JSON.parse(str);
function get_value(short) {
return terms.line_array.filter(function(o) {
return o.short == short
});
//in case the term isn't found, we'll prevent term.long from throwing an error
}
var result = get_value('_YES');
console.log(result);
</script>
var str='{“line_数组”:[{“short”:“[common],“long”:“undefined”},{“short”:“YES”,“long”:“YES”},{“short”:“NO”,“long”:“NO”},{“short”:“NOT”,“long”:“NOT”},{“short”:“SEARCH”,“long”:“SEARCH”},{“short”:“GO”,“long”:“GO”};
var terms=JSON.parse(str);
函数get_值(短){
返回项.行数组.过滤器(函数(o){
返回o.short==short
});
//如果找不到术语,我们将防止term.long抛出错误
}
var结果=获取值(“是”);
控制台日志(结果);
给出TypeError:termsDictionary在使用它的字典示例中未定义。*var项=str;var termsDictionary=terms.line_array.reduce(函数(d,t){d[t.short]=t.long;返回d;},Object.create(null));函数get_value(short){return termsDictionary[short];//您可以在不使用函数}console.log的情况下使用此表达式(get_value(“_YES”)*给出TypeError:terms.line_数组未定义[Learn More]var terms=JSON.parse(str)-您需要解析它。我将更新示例;to var terms=JSON.parse(str);现在它工作了……;-)
var haystack = {"line_array":[{"short":"[common]","long":"undefined"},{"short":"_YES","long":"Yes"},{"short":"_NO","long":"No"},{"short":"_NOT","long":"Not "},{"short":"_SEARCH","long":"Search"},{"short":"_GO","long":"Go"}]}
console.log(haystack.line_array.filter(e => e.short === '_YES').long)
<script>
var str = '{"line_array":[{"short":"[common]","long":"undefined"},{"short":"_YES","long":"Yes"},{"short":"_NO","long":"No"},{"short":"_NOT","long":"Not "},{"short":"_SEARCH","long":"Search"},{"short":"_GO","long":"Go"}]}';
var terms = JSON.parse(str);
function get_value(short) {
return terms.line_array.filter(function(o) {
return o.short == short
});
//in case the term isn't found, we'll prevent term.long from throwing an error
}
var result = get_value('_YES');
console.log(result);
</script>