2016-12-04 78 views
1

有一个JSON字符串(缩短):JavaScript的JSON阵列输出

{"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"}]} 

我想能够调用返回基于所述“短”的值的“长”值的函数:

like:

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]; 
} 

没有运气

回答

1

使用Array#find找到包含短值的对象。请注意,Array#find不受IE的支持。因此,如果您需要IE支持和/或您正在进行大量转换,则应该使用字典方式。

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) { 
 
    var term = terms.line_array.find(function(o) { 
 
    return o.short === short; 
 
    }); 
 
    
 
    //in case the term isn't found, we'll prevent term.long from throwing an error 
 
    return term && term.long; 
 
} 
 

 
var result = get_value('_YES'); 
 

 
console.log(result);

使用字典对象

创建字典,Array#reduce,并使用它:

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); 
 

 
var termsDictionary = terms.line_array.reduce(function(d, t) { 
 
    d[t.short] = t.long; 
 
    return d; 
 
}, Object.create(null)); 
 

 
function get_value(short) { 
 
    return termsDictionary[short]; // you can use this expression without the function of course 
 
} 
 

 
var result = get_value('_YES'); 
 

 
console.log(result);

+0

给出类型错误:termsDictionary未定义的词典范例 – osomanden

+0

使用它。 * var terms = str; var termsDictionary = terms.line_array.reduce(function(d,t){d.short] = t.long; return d; },Object。创建(空)); (短){ return termsDictionary [short]; //可以使用此表达而不当然 } 的console.log(的get_value( “_ YES”))的功能; * – osomanden

+0

给出了类型错误:terms.line_array是不确定的[了解详情] – osomanden

0

使用正确的解析JSON,您可以使用Array#find,一个ES6功能。

function getValue(short_string) { 
 
    return (object.line_array.find(a => a.short === short_string) || {}).long; 
 
} 
 

 
var json_string = '{"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"}]}', 
 
    object = JSON.parse(json_string), 
 
    test= 'Say '+getValue("_YES"); 
 

 
console.log(test);

ES5与Array#some

function getValue(short_string) { 
 
    var value; 
 
    object.line_array.some(function (a) { 
 
     if (a.short === short_string) { 
 
      value = a.long; 
 
      return true; 
 
     } 
 
    }); 
 
    return value; 
 
} 
 

 
var json_string = '{"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"}]}', 
 
    object = JSON.parse(json_string), 
 
    test= 'Say '+getValue("_YES"); 
 

 
console.log(test);

0

另一种选择:

function f_lang(short_string) { 
 
    var obj = {"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"}]}; 
 
    for (var i = 0; i < obj['line_array'].length; i++) { 
 
     if (obj['line_array'][i]['short'] == short_string) { 
 
     return obj['line_array'][i]['long']; 
 
     }; 
 
    } 
 
} 
 

 
console.log('Say ' + f_lang("_YES"));

0

您可以使用Array.Filter

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) 
0

请尝试以下代码

<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>