菜鸟教程小白 发表于 2022-12-13 03:04:06

iphone - 无法在 Objective-C 中访问 JSON 数据


                                            <p><p>好的,这是我在 Objective-C 中使用 JSON 的第一种方法(我对最后一种方法也很陌生)。我要获取存储在我的 json 中的信息以在 Objective-C 中使用它们,但是当尝试加载它时,我得到 null 以响应 <code>NSLog(@"%@",allData);</code> on。谁能告诉我我做错了什么?提前感谢您的时间和耐心。
哦,如果需要,这里是 json:
<a href="http://jsonviewer.stack.hu/#http://conqui.it/ricette.json" rel="noreferrer noopener nofollow">http://jsonviewer.stack.hu/#http://conqui.it/ricette.json</a> </p>

<pre><code>NSString *filePath = [ pathForResource:@&#34;recipes&#34; ofType:@&#34;json&#34;];

NSError *error = nil;

NSMutableData *JSONData = ;
NSLog(@&#34;%@&#34;,JSONData);


NSArray *allData = ;
NSLog(@&#34;%@&#34;,allData);

for (NSDictionary *diction in allData) {
    NSString *recipe = ;

    ;
}

NSLog(@&#34;%@&#34;,array);
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p><code>JSONObjectWithData</code> 方法有一个 <code>error</code> 参数,您可以利用该参数来诊断问题。例如:</p>

<pre><code>NSError *error = nil;
NSArray *allData = [NSJSONSerialization JSONObjectWithData:JSONData
                                                   options:0
                                                   error:&amp;error];
if (error)
    NSLog(@&#34;%s: JSONObjectWithData error: %@&#34;, __FUNCTION__, error);
</code></pre>

<p>在您的评论中,您建议您收到有关“字符 414 周围未转义的控制字符”的错误。这表明 JSON 本身存在错误,您可能希望通过将其复制到 <a href="http://jsonlint.com/" rel="noreferrer noopener nofollow">http://jsonlint.com/</a> 中来进行验证。看看它是否报告了任何问题。</p>

<p>针对是否存在任何 Objective-C 问题的更广泛的问题,<em>本身没有编码错误。</em>我无法评论 <code>for</code>循环,它清楚地假设 allData 是一个字典数组,如果没有看到 JSON,我就无法证明它。但我会相信你的话。但是,是的,Objective-C 代码看起来不错(尽管对返回值类型和错误对象的检查有点轻率)。 </p>

<p>例如,如果您想要一些可以在开发期间使用的诊断断言语句,您可以执行以下操作:</p>

<pre><code>NSArray *allData = ;
NSAssert(error, @&#34;%s: JSONObjectWithData error: %@&#34;, __FUNCTION__, error);

NSLog(@&#34;%s: array=%@&#34;, __FUNCTION__, array);

NSAssert(], @&#34;allData is not an array&#34;);

for (NSDictionary *diction in allData) {
    NSAssert(], @&#34;%s: diction is not a dictionary (%@), __FUNCTION__, diction);

    NSString *recipe = ;

    NSAssert(recipe, @&#34;%s: Did not find recipe key in diction (%@)&#34;, __FUNCTION__, diction);

    ;
}
</code></pre>

<p>如果这些错误中的任何一个可能是生产中的运行时错误,您应该将 assert 语句替换为执行必要错误处理的 <code>if</code> 语句。但希望它能说明这个概念。</p></p>
                                   
                                                <p style="font-size: 20px;">关于iphone - 无法在 Objective-C 中访问 JSON 数据,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/17516271/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/17516271/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iphone - 无法在 Objective-C 中访问 JSON 数据