2016-12-04 11 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でshort値を含むオブジェクトを見つけること。 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

下にしてみてくださいTypeError例外を与える:termsDictionaryは、それを使用した辞書の例 – osomanden

+0

に定義されていません。 * var terms = str; var termsDictionary = terms.line_array.reduce(function(d、t){ d [t.short] = t.long; return d; }、オブジェクト。create(null)); ファンクションget_value(short){ return termsDictionary [short]; //この式はもちろん、関数 なしで使用できます。 console.log(get_value( "_ YES")); * – osomanden

+0

はTypeErrorを返します。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);

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

別のオプションとES5:

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>