objective-c - iOS解析不同json字符串的正确条件
<p><p>我正在为用户身份验证系统解析 json。</p>
<p>如果登录成功,服务器会发送以下用户对象;</p>
<pre><code>{
"users": {
"id": "1",
"FB_first_name": null,
"FB_last_name": null,
"FB_gender": null,
"FB_id": null,
"FB_link": null,
"FB_locale": null,
"FB_name": null,
"FB_username": null,
"created_at": "2011-01-02 08:30:59 UTC",
"updated_at": "2011-01-02 08:31:23 UTC",
"email": "[email protected]",
"language_id": "1"
}
}
</code></pre>
<p>我使用以下代码解析;</p>
<pre><code> NSDictionary *userDic = (NSDictionary*)[(NSDictionary*)results objectForKey:@"users"];
User *aUser = [ initWithID:
withUserName:
withUserEmail:
withUserFName:];
</code></pre>
<p>但如果用户无效,服务器会发送以下响应;</p>
<pre><code>[
"Invalid Email or Password."
]
</code></pre>
<p>我应该使用什么 <code>if()</code> 语句来解析和显示两种情况下的响应?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><pre><code>//parse out the json data
NSError* error;
id json = ;
if (){
//You have to be sure that on error it returns array not a dictionary
NSArray *arr = (NSArray*)json;
NSString *errorMsg = ;
}
else {
}
</code></pre></p>
<p style="font-size: 20px;">关于objective-c - iOS解析不同json字符串的正确条件,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/13821470/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/13821470/
</a>
</p>
页:
[1]