使用 Objective-C 从 json 检索值
我目前正在尝试使用 json 和 Objective-C,但是遇到了一些困难。以下是返回的 json,
{
sethostname = (
{
msgs = "Updating Apache configuration\nUpdating cPanel license...Done. Update succeeded.\nBuilding global cache for cpanel...Done";
status = 1;
statusmsg = "Hostname Changed to: a.host.name.com";
warns = (
);
});
}
我可以检查响应是否返回,并且密钥是 sethostname,但是无论我尝试什么,我都无法获取例如 status 或 statusmsg 的值。谁能指出我正确的位置。以下是我用来检查是否返回 sethostname 的基本代码。
NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&myError];
NSLog([res description]);
NSArray *arr;
arr = [res allKeys];
if ([arr containsObject:@"sethostname"])
{
NSLog(@"worked");
}
I am currently trying to work with json and objective-c however having a bit of difficulty. The following is the json that is being returned
{
sethostname = (
{
msgs = "Updating Apache configuration\nUpdating cPanel license...Done. Update succeeded.\nBuilding global cache for cpanel...Done";
status = 1;
statusmsg = "Hostname Changed to: a.host.name.com";
warns = (
);
});
}
I am able to check that the response is coming back and the key is sethostname however no matter what I try I cannot get for example the value of status or statusmsg. Can anyone point me in the right location. The following is basic code I am using to check that sethostname is returned.
NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&myError];
NSLog([res description]);
NSArray *arr;
arr = [res allKeys];
if ([arr containsObject:@"sethostname"])
{
NSLog(@"worked");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如有疑问,请记下 JSON 数据的结构。例如:(
实际上是 NeXTSTEP 属性列表格式)意味着您有一个顶级字典。这个顶级字典包含一个名为
sethostname
的键,其值是一个数组。该数组由字典组成,每个字典都有一组键:msgs、status、statusmsg、warns。msgs
有一个字符串值,status
有一个数字值,statusmsg
有一个字符串值,
warns` 有一个数组值:理解了这个结构后,您的代码应该如下所示:
When in doubt, write down the structure of your JSON data. For example:
(which is in NeXTSTEP property list format, actually) means that you have a top-level dictionary. This top-level dictionary contains a key called
sethostname
whose value is an array. This array is comprised of dictionaries, each dictionary having a set of keys:msgs, status, statusmsg, warns
.msgs
has a string value,status
has a number value,statusmsg
has a string value,
warns` has an array value:Having understood this structure, your code should look like:
为什么不使用最简单的 JSON 方法 -
[myString jsonValue];
它是此 JSON 的一部分Objective-c 框架
Why not use the simplest JSON method -
[myString jsonValue];
It's part of this JSON framework for objective-c
我认为
if ([arr containsObject:@"sethostname"])
不会起作用,因为结果数组不会包含该确切的对象。它可能包含具有相同内容的对象,但它不会是同一个对象。正如 jtbandes 所写,您需要记录实际输出。 NSLog res 和 arr 并看看你有什么。
I don't think
if ([arr containsObject:@"sethostname"])
is going to work, because the results array is not going to contain that exact object. It might contain an object with the same content, but it won't be the SAME object.As jtbandes wrote, you need to log the actually output. NSLog both res and arr and see what you have.