Javascript错误中未定义的JSON值

时间:2015-07-09 20:45:06

标签: javascript json

我正在阅读JSON对象,尝试遍历文本并创建文本的子字符串。由于某种原因,它回归"无法读取属性'角色'未定义的。"我错过了什么?

  // target language text
var text1 =  obj[0].tgtLanguageSentences[0].text;

var strArry1 = [];  
var colorArr1 = ["DarkSalmon", "ForestGreen", "Brown"];
for(var t=0; t<obj[0].tgtLanguageSentences[0].roles.length; t++)
             {
// create variables representing substrings of the Source language Sentence
var tgt1 = text1.substring((obj[t].tgtLanguageSentences[t].roles[t].beginOffset - obj[t].tgtLanguageSentences[t].roles[t].beginOffset),(obj[t].tgtLanguageSentences[t].roles[t].beginOffset - 1));
var tgt2 = text1.substring(obj[t].tgtLanguageSentences[t].roles[t].beginOffset,obj[t].tgtLanguageSentences[t].roles[t].endOffset);
var tgt3 =
text1.substring(obj[t].tgtLanguageSentences[t].roles[t].endOffset,obj[t].tgtLanguageSentences[t].text.length);

strArry.push('<h4>'+tgt1+'</h4>');
strArry.push('<h4>'+'<font color="'+colorArr1[i]+'">"'+tgt2+'</font>'+'</h4>');

if(i == obj[0].tgtLanguageSentences[0].roles[0].length-1)
                {
                    strArry.push('<h4>'+tgt3+'</h4>');
                }

                 text1 =  s3;
             }

请参阅下面的参考资料中的JSON对象:

[
  {
    "description": "",
    "roles": [
      {
        "name": "thing commented on"
      },
      {
        "name": "commentor"
      }
    ],
    "srcLanguageSentence": {
      "roles": [
        {
          "beginOffset": 23,
          "endOffset": 30,
          "name": "thing commented on",
          "text": "on them"
        },
        {
          "beginOffset": 5,
          "endOffset": 7,
          "name": "commentor",
          "text": "We"
        }
      ],
      "text": "  `` We wo n't comment on them . '' ",
      "verb": {
        "beginOffset": 15,
        "endOffset": 22,
        "text": "comment"
      }
    },
    "tgtLanguageSentences": [
      {
        "roles": [
          {
            "beginOffset": 1,
            "endOffset": 31,
            "name": "thing commented on",
            "text": "Weitere Aspekte der Kommission"
          },
          {
            "beginOffset": 44,
            "endOffset": 47,
            "name": "commentor",
            "text": "ich"
          },
          {
            "beginOffset": 48,
            "endOffset": 55,
            "name": "negation",
            "text": "nicht ."
          }
        ],
        "text": "  Weitere Aspekte der Kommission kommentiere ich nicht . ",
        "verb": {
          "beginOffset": -1,
          "endOffset": -1,
          "sense": "COMMENT, intransitive",
          "text": "kommentieren"
        }
      }
    ],
    "verb": "KOMMENTIEREN"
  }
]

3 个答案:

答案 0 :(得分:0)

将所有引用更改为

InvCipher(byte in[4*Nb], byte out[4*Nb], word w[Nb*(Nr+1)]) begin byte state[4,Nb] state = in AddRoundKey(state, w[Nr*Nb, (Nr+1)*Nb-1]) // See Sec. 5.1.4 for round = Nr-1 step -1 downto 1 InvShiftRows(state) InvSubBytes(state) AddRoundKey(state, w[round*Nb, (round+1)*Nb-1]) InvMixColumns(state) end for InvShiftRows(state) InvSubBytes(state) AddRoundKey(state, w[0, Nb-1]) out = state end

tooltip

答案 1 :(得分:0)

here is the problem: In below snippet code you are looping through the role but in substring you passing obj[t]. As per above given JSON you have only one item in obj however in role you have more than 1 item.

for(var t=0; t<obj[0].tgtLanguageSentences[0].roles.length; t++)
             {
// create variables representing substrings of the Source language Sentence
var tgt1 = text1.substring((obj[t].tgtLanguageSentences[t].roles[t].beginOffset - obj[t].tgtLanguageSentences[t].roles[t].beginOffset),(obj[t].tgtLanguageSentences[t].roles[t].beginOffset - 1));
var tgt2 = text1.substring(obj[t].tgtLanguageSentences[t].roles[t].beginOffset,obj[t].tgtLanguageSentences[t].roles[t].endOffset);
var tgt3 =
text1.substring(obj[t].tgtLanguageSentences[t].roles[t].endOffset,obj[t].tgtLanguageSentences[t].text.length);

Answere is:

var tgt1 = text1.substring((obj[0].tgtLanguageSentences[0].roles[t].beginOffset - obj[0].tgtLanguageSentences[0].roles[t].beginOffset),(obj[0].tgtLanguageSentences[0].roles[t].beginOffset - 1));

Hope this will help you :)

答案 2 :(得分:0)

Would be helpful to know, from debugging, which line is throwing the error. But it's probably this one:

var tgt1 = text1.substring((obj[t].tgtLanguageSentences[t].roles[t].beginOffset - obj[t].tgtLanguageSentences[t].roles[t].beginOffset),(obj[t].tgtLanguageSentences[t].roles[t].beginOffset - 1));

Using t to index obj and tgtLAnguageSentences and roles is problematic based on your JSON object example. The loop will run 4 times based on t<obj[0].tgtLanguageSentences[0].roles.length.

When t = 1, for instance, you are trying to find a value at obj[1].tgtLanguageSentences[1].roles[1] if there is no object at obj[1].tgtLanguageSentences[1] then you will get "cannot read property 'roles' of undefined."