2014-11-05 70 views
-1

這裏其中我的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); 

的日誌值返回以下文本

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

我不明白爲什麼它保持顯示這麼多空值而不是o f顯示當前值。我的代碼有什麼問題?如何獲得唯一的值?請更正我的代碼

+0

你解析過JSON響應嗎? – Kampai 2014-11-05 13:14:03

+0

是的,我解析了JSON – Fazil 2014-11-05 13:14:32

+1

讓我們只說「彈出窗口」。它返回一個數組,而不是一個字典。將這個值保存到'NSArray'中,然後得到更多。 – Kampai 2014-11-05 13:15:43

回答

1

您的JSON中的'popups`實際上是一個不是字典的數組。

數組中的第一個元素是一本字典那麼這將是你的代碼的有效

NSDictionary *popupContents = [readingPlanContents valueForKeyPath:@"popups"][0]; 
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 
    } 
+0

內容即將到來,但仍在循環顯示空值 – Fazil 2014-11-05 13:58:44