如何使用Javascript警报显示特定的JSON数据?

时间:2018-04-23 12:57:39

标签: javascript json

这是我的JSON数据:

{
  "status": "Succeeded",
  "recognitionResult": {
    "lines": [
      {
        "boundingBox": [
          2,
          52,
          65,
          46,
          69,
          89,
          7,
          95
        ],
        "text": "The quick brown fox jumps over the lazy",
        }
    ]
}

我想从这个JSON数据中提取文本。我该怎么做?

3 个答案:

答案 0 :(得分:0)

您可以尝试以下方式:

var myJSON = {
  "status": "Succeeded",
  "recognitionResult": {
    "lines": [
      {
        "boundingBox": [
          2,
          52,
          65,
          46,
          69,
          89,
          7,
          95
        ],
        "text": "The quick brown fox jumps over the lazy",
        }
  ]
}
}

var text = myJSON.recognitionResult.lines[0].text;
console.log(text)

答案 1 :(得分:0)

var status = myJSON.status;
alert( status );

var text = myJSON.recognitionResult.lines[0].text;
alert( text );

答案 2 :(得分:0)

让代码考虑动态行为总是更好。如果您的lines数组有多个对象怎么办?因此,我建议在这种情况下使用循环,尽管甚至有一个对象。



var jsonData = {
  "status": "Succeeded",
  "recognitionResult": {
    "lines": [
      {
        "boundingBox": [
          2,
          52,
          65,
          46,
          69,
          89,
          7,
          95
        ],
        "text": "The quick brown fox jumps over the lazy"
      },
       {
        "boundingBox": [
          2,
          52,
          65,
          95
        ],
        "text": "fox jumps over the lazy"
      }
    ]
  }
};
jsonData.recognitionResult.lines.forEach(function(line){
  console.log(line.text);
});