解析json不工作显示double

时间:2014-12-28 20:03:58

标签: javascript jquery json

以下是作为json返回的内容,存储在data

    {
   "dialogue":{
      "id":"1",
      "backgroundURL":"http:\/\/db5mq3uumohzn.cloudfront.net\/course\/images\/english\/d-backgrounds\/onboard-metro.jpg"
   },
   "dialogueTexts":[
      {
         "dialogueTextId":"12",
         "dialogueTextSpeaker":"1",
         "dialogueTextText":"Hello, Paul! How are you?",
         "dialogueTextAudioURL":"http:\/\/db5mq3uumohzn.cloudfront.net\/course\/audio\/english\/1\/1\/gina_0.mp3",
         "dialogueTextImageURL":"http:\/\/db5mq3uumohzn.cloudfront.net\/course\/images\/english\/people\/female2.png",
         "dialogueTextDialogueID":"1"
      },
      {
         "dialogueTextId":"13",
         "dialogueTextSpeaker":"2",
         "dialogueTextText":"Hi, Anne! I'm fine, thanks. And you?",
         "dialogueTextAudioURL":"http:\/\/db5mq3uumohzn.cloudfront.net\/course\/audio\/english\/1\/1\/l1-01.mp3",
         "dialogueTextImageURL":"http:\/\/db5mq3uumohzn.cloudfront.net\/course\/images\/english\/people\/male14.png",
         "dialogueTextDialogueID":"1"
      },
      {
         "dialogueTextId":"14",
         "dialogueTextSpeaker":"1",
         "dialogueTextText":"I'm well. Thank you.",
         "dialogueTextAudioURL":"http:\/\/db5mq3uumohzn.cloudfront.net\/course\/audio\/english\/1\/1\/gina_1.mp3",
         "dialogueTextImageURL":"http:\/\/db5mq3uumohzn.cloudfront.net\/course\/images\/english\/people\/female2.png",
         "dialogueTextDialogueID":"1"
      },
      {
         "dialogueTextId":"15",
         "dialogueTextSpeaker":"2",
         "dialogueTextText":"It's good to see you!",
         "dialogueTextAudioURL":"http:\/\/db5mq3uumohzn.cloudfront.net\/course\/audio\/english\/1\/1\/l1-02.mp3",
         "dialogueTextImageURL":"http:\/\/db5mq3uumohzn.cloudfront.net\/course\/images\/english\/people\/male14.png",
         "dialogueTextDialogueID":"1"
      },
      {
         "dialogueTextId":"16",
         "dialogueTextSpeaker":"1",
         "dialogueTextText":"Yes. You too! Good-bye!",
         "dialogueTextAudioURL":"http:\/\/db5mq3uumohzn.cloudfront.net\/course\/audio\/english\/1\/1\/gina_2.mp3",
         "dialogueTextImageURL":"http:\/\/db5mq3uumohzn.cloudfront.net\/course\/images\/english\/people\/female2.png",
         "dialogueTextDialogueID":"1"
      },
      {
         "dialogueTextId":"17",
         "dialogueTextSpeaker":"2",
         "dialogueTextText":"Bye! See you later!",
         "dialogueTextAudioURL":"http:\/\/db5mq3uumohzn.cloudfront.net\/course\/audio\/english\/1\/1\/l1-03.mp3",
         "dialogueTextImageURL":"http:\/\/db5mq3uumohzn.cloudfront.net\/course\/images\/english\/people\/male14.png",
         "dialogueTextDialogueID":"1"
      }
   ]
}

以下是我的剧本:

$.each( data, function( key, val ) {

            alert(data['dialogueTexts']['dialogueTextText'])

            if(data['dialogue']['backgroundURL'] != ''){

                $('#content-container').css('background-image','url("'+data['dialogue']['backgroundURL']+'")');
                alert(data['dialogue']['backgroundURL']);

            }else{
                alert('test');
            }
        });

我似乎无法进入对话点,并且它保持警惕backgroundURL来自对话,我不知道为什么它循环。在第二次交互中能够进入dialogTexts然后循环遍历它我做错了什么?

1 个答案:

答案 0 :(得分:0)

假设数据是您在上面发布的所有JSON,这是一个版本,也许是您想要的:

$.each( data['dialogueTexts'], function( key, val ) {         
       alert(val['dialogueTextText']); 
});

if(data['dialogue']['backgroundURL'] != '')
       $('#content-container').css('background-image','url("'+data['dialogue']['backgroundURL']+'")');