<bdo id='INjmN'></bdo><ul id='INjmN'></ul>

    <legend id='INjmN'><style id='INjmN'><dir id='INjmN'><q id='INjmN'></q></dir></style></legend>
  • <i id='INjmN'><tr id='INjmN'><dt id='INjmN'><q id='INjmN'><span id='INjmN'><b id='INjmN'><form id='INjmN'><ins id='INjmN'></ins><ul id='INjmN'></ul><sub id='INjmN'></sub></form><legend id='INjmN'></legend><bdo id='INjmN'><pre id='INjmN'><center id='INjmN'></center></pre></bdo></b><th id='INjmN'></th></span></q></dt></tr></i><div id='INjmN'><tfoot id='INjmN'></tfoot><dl id='INjmN'><fieldset id='INjmN'></fieldset></dl></div>

    1. <small id='INjmN'></small><noframes id='INjmN'>

        <tfoot id='INjmN'></tfoot>

        将 JSON 数组解析为 NSDictionary

        时间:2023-06-01
        <legend id='DttQS'><style id='DttQS'><dir id='DttQS'><q id='DttQS'></q></dir></style></legend>
      1. <i id='DttQS'><tr id='DttQS'><dt id='DttQS'><q id='DttQS'><span id='DttQS'><b id='DttQS'><form id='DttQS'><ins id='DttQS'></ins><ul id='DttQS'></ul><sub id='DttQS'></sub></form><legend id='DttQS'></legend><bdo id='DttQS'><pre id='DttQS'><center id='DttQS'></center></pre></bdo></b><th id='DttQS'></th></span></q></dt></tr></i><div id='DttQS'><tfoot id='DttQS'></tfoot><dl id='DttQS'><fieldset id='DttQS'></fieldset></dl></div>
        <tfoot id='DttQS'></tfoot>
        • <bdo id='DttQS'></bdo><ul id='DttQS'></ul>

            <tbody id='DttQS'></tbody>

            <small id='DttQS'></small><noframes id='DttQS'>

                  本文介绍了将 JSON 数组解析为 NSDictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在使用 Wea​​ther Underground API 制作一个应用程序,但在解析与严重警报相关的块时遇到了障碍.JSON 使用具有子键值对的键值对——这对我来说不是问题,因为我可以从中制作后续的 NSDictionaries——但严重警报的条目已被证明是有问题的.见下文:

                  I'm working with the Weather Underground API to make an app and I've hit a snag while parsing the block relating to severe alerts. The JSON uses key-value pairs that have sub key value pairs -- which haven't been a problem for me, as I can make subsequent NSDictionaries out of those -- but the entry for severe alerts has proven problematic. See below:

                  "alerts": [
                      {
                      "type": "WAT",
                      "description": "Flash Flood Watch",
                      "date": "3:13 PM EDT on April 28, 2012",
                      "date_epoch": "1335640380",
                      "expires": "8:00 AM EDT on April 29, 2012",
                      "expires_epoch": "1335700800",
                      "message": "u000A...Flash Flood Watch in effect through Sunday morning...u000Au000AThe National Weather Service in Charleston has issued au000Au000A* Flash Flood Watch for portions of northeast Kentucky... (Note: I trimmed this for length's sake),
                      "phenomena": "FF",
                      "significance": "A"
                      }
                  ]
                  

                  警报"对与我能够解析的其他对不同,因为它有一个围绕子值的 [ ] 括号,我不知道如何清除它,所以我可以访问子值.在我能够解析的其他示例中,它只有 { } 括号,而不是 { } 和 [ ] 括号.作为参考,括号始终存在——即使没有恶劣天气警报……在这种情况下,警报"对返回括号 [ ],不存在子对.

                  The "alerts" pair differs from others I've been able to parse because it has this [ ] bracket surrounding the sub-values and I'm not sure how to clear it so I can access the subvalues. In the other examples I've been able to parse, it only has the { } brackets, and not both the { } and [ ] brackets. For reference, the brackets are always present -- even when there are no severe weather alerts... in that instance the "alerts" pair returns the brackets [ ] with no sub-pairs present.

                  有没有办法可以从 NSDictionary 中删除 [ ] 括号,或者忽略它们?任何建议将不胜感激!

                  Is there a way I can remove the [ ] brackets from the NSDictionary, or otherwise ignore them? Any advice would be appreciated!

                  对于参考和故障排除帮助,以下是我成功解析 JSON 文档其余部分的方法:

                  For reference and troubleshooting help, here's how I'm parsing the rest of the JSON document successfully:

                  1) 从原始 JSON 创建一个 NSDictionary

                  1) Create an NSDictionary from the raw JSON

                  //Process Weather Call
                  NSError* error;
                  NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
                  

                  2) 为嵌套的 json 对创建后续字典

                  2) Create subsequent dictionaries for nested json pairs

                  NSDictionary *current_observation = [json objectForKey:@"current_observation"];
                  

                  3) 赋值

                  NSString* weather;
                  weather = [current_observation objectForKey:@"weather"];
                  

                  所以最终结果将是一个字符串,上面写着部分多云"或其他内容,以及许多我没有显示的相关天气值.这些解析成功,因为它们只有范围括号 {},而不是 [] 括号.

                  So the end result would be a string that says "Partly Cloudy" or something, along with numerous related weather values that I haven't shown. These parse successfully because they only have the scope brackets { }, and not the [ ] brackets.

                  推荐答案

                  括号表示数组中的Json数据.你可以如下解析它

                  The brackets means the Json data there are in an array. You can parse it as following

                  NSArray *alertArray = [json objectForKey:@"alerts"];
                  

                  现在您应该遍历所有警报并解析它们(在您的情况下它只有 1 个,但在另一个 json 字符串中可能更多):

                  now you should loop through all alerts and parse them (in your case it's only 1, but it could be more in another json string):

                  //parse each alert
                  for (NSDictionary *alert in alertArray ){
                       NSString* description = [alert  objectForKey:@"description"];
                      //etc...
                  }
                  

                  这篇关于将 JSON 数组解析为 NSDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在大型 UIScrollView 中直观地排列子视图 下一篇:强制 iOS 应用以横向模式启动

                  相关文章

                  最新文章

                1. <small id='XYuLp'></small><noframes id='XYuLp'>

                2. <i id='XYuLp'><tr id='XYuLp'><dt id='XYuLp'><q id='XYuLp'><span id='XYuLp'><b id='XYuLp'><form id='XYuLp'><ins id='XYuLp'></ins><ul id='XYuLp'></ul><sub id='XYuLp'></sub></form><legend id='XYuLp'></legend><bdo id='XYuLp'><pre id='XYuLp'><center id='XYuLp'></center></pre></bdo></b><th id='XYuLp'></th></span></q></dt></tr></i><div id='XYuLp'><tfoot id='XYuLp'></tfoot><dl id='XYuLp'><fieldset id='XYuLp'></fieldset></dl></div>
                    <legend id='XYuLp'><style id='XYuLp'><dir id='XYuLp'><q id='XYuLp'></q></dir></style></legend>

                    1. <tfoot id='XYuLp'></tfoot>
                        <bdo id='XYuLp'></bdo><ul id='XYuLp'></ul>