2016-12-04 75 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>