Dictionary无任何理由返回null值

时间:2014-11-05 13:09:19

标签: ios json dictionary nsdictionary

这里我的JSON格式来自web-service作为响应,

"popups":
 [
 {
  "sowhat": 
            {
                "isAvailable": "TRUE",
                "hasVideo": "FALSE",
                "hasAudio": "TRUE",
                "content": "<p><span><span><strong>SO WHAT?</strong></span></span></p><p><span><span> Belief shapes behaviour. But the question is, &lsquo;How much?&rsquo; How much does the promised return of Jesus shape your behaviour from day to day? Do you find yourself thinking about it very much or maybe doubting it from time to time? Perhaps we can liken it to the final whistle or siren of a grand final. The whistle ends the match but in a much greater way the return of Jesus ends world history as we know it. His return ushers in the complete rule of God&rsquo;s kingdom which began when Jesus came to earth. Does this reality shape what you do each day? Are you ready for his return?</span></span></p>"
            }
 },
 {
  "questions": 
            {
                "isAvailable": "TRUE",
                "hasVideo": "FALSE",
                "hasAudio": "FALSE",
                "content": "<p><span><span><strong>QUESTIONS </strong></span></span></p><ol><li><p><span><span>Read Luke 1:1-4. How is Acts both similar and different to Luke&rsquo;s gospel?</span></span></p></li><li><p><span><span>What key Christian truths are mentioned in Acts 1:1-11?</span></span></p></li><li><p><span><span>How is the Holy Spirit related to the task assigned to the apostles (verse 8)?</span></span></p></li><li><p><span><span>If verse 8 hints at the broad outline of the book of Acts, where might the gospel arrive at by the conclusion of the book?</span></span></p></li><li><p><span><span>What does the choosing of Matthias demonstrate about the disciples&rsquo; attitude to the Scriptures?</span></span></p></li></ol>"
            }
  }
]

这是我用于在字典中解析的代码

  NSDictionary *popupContents = [readingPlanContents valueForKeyPath:@"popups"];
  //SoWhatPopup
  NSDictionary *soWhatPopup = [popupContents valueForKey:@"sowhat"];
  soWhatPopupAvlbl=[soWhatPopup valueForKey:@"isAvailable"];
  NSString *soWhatVdeo=[soWhatPopup valueForKey:@"hasVideo"];
  NSString *soWhatAdio=[soWhatPopup valueForKey:@"hasAudio"];
  soWhatCntn=[soWhatPopup valueForKey:@"content"];
  NSLog(@"soWhatVdeo:%@ ",soWhatVdeo);
  NSLog(@"soWhatAdio:%@ ",soWhatAdio);
  NSLog(@"soWhatCntn:%@ ",soWhatCntn);
  NSLog(@"soWhatPopupAvlbl:%@ ",soWhatPopupAvlbl);
//QuestionPopup
  NSDictionary *questPopup = [popupContents valueForKeyPath:@"questions"];
  questPopupAvlbl=[questPopup valueForKey:@"isAvailable"];
  NSString *questVdeo=[questPopup valueForKey:@"hasVideo"];
  NSString *questAdio=[questPopup valueForKey:@"hasAudio"];
  questCntn=[questPopup valueForKey:@"content"];
  NSLog(@"Avaliable:%@ Content:%@",questPopupAvlbl,questCntn);

Log值返回以下文本

soWhatVdeo:
(
FALSE,
"<null>",
"<null>",
"<null>",
"<null>"
) 

2014-11-05 17:24:21.044 StandFirm[3655:100200] soWhatAdio:(
TRUE,
"<null>",
"<null>",
"<null>",
"<null>"

 2014-11-05 17:24:21.044 StandFirm[3655:100200] soWhatCntn:(
 "<p><span><span><strong>SO WHAT?</strong></span></span></p><p><span><span> Belief shapes behaviour. But the question is, &lsquo;How much?&rsquo; How much does the promised return of Jesus shape your behaviour from day to day? Do you find yourself thinking about it very much or maybe doubting it from time to time? Perhaps we can liken it to the final whistle or siren of a grand final. The whistle ends the match but in a much greater way the return of Jesus ends world history as we know it. His return ushers in the complete rule of God&rsquo;s kingdom which began when Jesus came to earth. Does this reality shape what you do each day? Are you ready for his return?</span></span></p>",
 "<null>",
 "<null>",
 "<null>",
 "<null>"
 ) 
2014-11-05 17:24:21.045 StandFirm[3655:100200] soWhatPopupAvlbl:(
TRUE,
"<null>",
"<null>",
"<null>",
"<null>"
) 

我不明白为什么它会显示如此多的空值而不是显示当前值。我的代码出了什么问题?如何只得到值?请更正我的代码

2 个答案:

答案 0 :(得分:1)

你的JSON中的'popups`实际上是一个数组而不是一个字典。

数组中的第一个元素是字典,所以这是有效的

NSDictionary *popupContents = [readingPlanContents valueForKeyPath:@"popups"][0];

答案 1 :(得分:0)

更改代码的第一行。这里readingPlanContents返回一个数组而不是字典。因此,您需要为此创建一个NSArray变量,并使用for循环来获取内部值。见下面的代码。

    NSArray *popupContents = [readingPlanContents valueForKeyPath:@"popups"];

    for (NSDictionary *details in popupContents) {
        NSDictionary *soWhatPopup = [details valueForKey:@"sowhat"];
        soWhatPopupAvlbl=[soWhatPopup valueForKey:@"isAvailable"];
        //....

        // And so on
    }