【问题标题】:Dictionary returns null value without any reason字典无故返回空值
【发布时间】:2015-01-01 16:40:17
【问题描述】:

这是我的 JSON 格式,它是从 Web 服务作为响应获得的,

"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>"
) 

我不明白为什么它保持显示这么多空值而不是显示当前值。我的代码有什么问题?如何只获取值?请更正我的代码

【问题讨论】:

  • 你解析 JSON 响应了吗?
  • 让我们谈谈“弹出窗口”。它返回一个数组,而不是字典。将此值保存到NSArray,然后再进一步。
  • @Kampai 绝对正确

标签: ios json dictionary nsdictionary


【解决方案1】:

更改代码的第一行。这里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
    }

【讨论】:

  • 内容即将到来,但在循环时仍显示空值
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-14
    • 1970-01-01
    • 2021-04-22
    • 1970-01-01
    • 1970-01-01
    • 2013-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多