ios - 如何使用具有字典数组的键解析字典
<p><p>UPDATE-8/31/12-所以现在我需要通过基于“isReservable”= 1 的字典数组,然后显示“开始”。我只是想获取每个字典数组的内容,我可以从那里开始(现在)。以下是我尝试过的。感谢大家的帮助,我每天都在学习更多知识。</p>
<p>来自php页面的json</p>
<pre><code>{
"slots": {
"2012-08-31 00:00:00 America/Los_Angeles": [
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-08-30 17:00:00 America/Los_Angeles",
"end": "2012-08-30 17:30:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-08-30 17:30:00 America/Los_Angeles",
"end": "2012-08-30 18:00:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": false,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-08-30 18:00:00 America/Los_Angeles",
"end": "2012-08-31 00:00:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": false,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-08-31 00:00:00 America/Los_Angeles",
"end": "2012-08-31 08:00:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-08-31 08:00:00 America/Los_Angeles",
"end": "2012-08-31 08:30:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-08-31 08:30:00 America/Los_Angeles",
"end": "2012-08-31 09:00:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-08-31 09:00:00 America/Los_Angeles",
"end": "2012-08-31 09:30:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-08-31 09:30:00 America/Los_Angeles",
"end": "2012-08-31 10:00:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": false,
"isReserved": true,
"label": "Thomas Kroom",
"begin": "2012-08-31 10:00:00 America/Los_Angeles",
"end": "2012-08-31 12:30:00 America/Los_Angeles",
"span": 5
},
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-08-31 12:30:00 America/Los_Angeles",
"end": "2012-08-31 13:00:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-08-31 13:00:00 America/Los_Angeles",
"end": "2012-08-31 13:30:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-08-31 13:30:00 America/Los_Angeles",
"end": "2012-08-31 14:00:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-08-31 14:00:00 America/Los_Angeles",
"end": "2012-08-31 14:30:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-08-31 14:30:00 America/Los_Angeles",
"end": "2012-08-31 15:00:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-08-31 15:00:00 America/Los_Angeles",
"end": "2012-08-31 15:30:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-08-31 15:30:00 America/Los_Angeles",
"end": "2012-08-31 16:00:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-08-31 16:00:00 America/Los_Angeles",
"end": "2012-08-31 16:30:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-08-31 16:30:00 America/Los_Angeles",
"end": "2012-08-31 17:00:00 America/Los_Angeles",
"span": 1
}
],
"2012-09-01 00:00:00 America/Los_Angeles": [
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-08-31 17:00:00 America/Los_Angeles",
"end": "2012-08-31 17:30:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-08-31 17:30:00 America/Los_Angeles",
"end": "2012-08-31 18:00:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": false,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-08-31 18:00:00 America/Los_Angeles",
"end": "2012-09-01 00:00:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": false,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-09-01 00:00:00 America/Los_Angeles",
"end": "2012-09-01 08:00:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-09-01 08:00:00 America/Los_Angeles",
"end": "2012-09-01 08:30:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-09-01 08:30:00 America/Los_Angeles",
"end": "2012-09-01 09:00:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-09-01 09:00:00 America/Los_Angeles",
"end": "2012-09-01 09:30:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-09-01 09:30:00 America/Los_Angeles",
"end": "2012-09-01 10:00:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-09-01 10:00:00 America/Los_Angeles",
"end": "2012-09-01 10:30:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-09-01 10:30:00 America/Los_Angeles",
"end": "2012-09-01 11:00:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-09-01 11:00:00 America/Los_Angeles",
"end": "2012-09-01 11:30:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-09-01 11:30:00 America/Los_Angeles",
"end": "2012-09-01 12:00:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-09-01 12:00:00 America/Los_Angeles",
"end": "2012-09-01 12:30:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-09-01 12:30:00 America/Los_Angeles",
"end": "2012-09-01 13:00:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-09-01 13:00:00 America/Los_Angeles",
"end": "2012-09-01 13:30:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-09-01 13:30:00 America/Los_Angeles",
"end": "2012-09-01 14:00:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-09-01 14:00:00 America/Los_Angeles",
"end": "2012-09-01 14:30:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-09-01 14:30:00 America/Los_Angeles",
"end": "2012-09-01 15:00:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-09-01 15:00:00 America/Los_Angeles",
"end": "2012-09-01 15:30:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-09-01 15:30:00 America/Los_Angeles",
"end": "2012-09-01 16:00:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-09-01 16:00:00 America/Los_Angeles",
"end": "2012-09-01 16:30:00 America/Los_Angeles",
"span": 1
},
{
"isPending": false,
"isReservable": true,
"isReserved": false,
"label": "&nbsp;",
"begin": "2012-09-01 16:30:00 America/Los_Angeles",
"end": "2012-09-01 17:00:00 America/Los_Angeles",
"span": 1
}
]
}
</code></pre>
<p>这是我目前所拥有的</p>
<pre><code>- (void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
//NSLog(@"string is %@", responseData);
NSDictionary *json = ;
NSDictionary* myslots =;
NSLog(@"allslots: %@", myslots);
//for (NSString *slotKey in myslots.allKeys) {
//NSDictionary *slot = ;
//NSLog(@"These are all the keys: %@", slotKey);
NSArray *keys = ;
</code></pre>
<p>更新-这就是我让它工作的方式:</p>
<pre><code>for (NSString *slotKey in myslots.allKeys) {
NSDictionary *slot = ;
NSArray *tests = ;
for (NSDictionary *myDict in tests){
NSLog(@"%@", );
}
}
</code></pre>
<p>现在我只需要把它改成 if "isReservable"= true then display "begin"</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>看起来 myslots 是一个 NSDictionary,而不是一个 NSArray ...要遍历它的所有键,更改</p>
<pre><code>for (NSDictionary *slots in myslots) {
// code
}
</code></pre>
<p>为</p>
<pre><code>for (NSString *slotKey in myslots.allKeys) {
NSDictionary *slot = ;
// code
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何使用具有字典数组的键解析字典,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/12204006/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/12204006/
</a>
</p>
页:
[1]