Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/122.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从JSON iOS中提取值_Php_Ios_Objective C_Json - Fatal编程技术网

Php 从JSON iOS中提取值

Php 从JSON iOS中提取值,php,ios,objective-c,json,Php,Ios,Objective C,Json,我正在开发一个与我自己的服务器(Apache->PHP)通信的应用程序,我正在从服务器向应用程序发送一个包含PHP代码的简单JSON字典: $aJSON = array('Result' => TRUE, 'Result_Msg' => "Success!!!"); echo json_encode($aJSON); 通过这些代码行,我想从键“Result”和“Result\u Msg”中提取值: // Logging the received and converted JSON

我正在开发一个与我自己的服务器(Apache->PHP)通信的应用程序,我正在从服务器向应用程序发送一个包含PHP代码的简单JSON字典:

$aJSON = array('Result' => TRUE, 'Result_Msg' => "Success!!!");
echo json_encode($aJSON);
通过这些代码行,我想从键“Result”和“Result\u Msg”中提取值:

// Logging the received and converted JSON/NSMutableDictionary
NSLog(@"responseDict=%@",dJSON);

BOOL b = [dJSON objectForKey:@"Response"];
NSString *string = [dJSON objectForKey:@"Result_Msg"];

NSLog(@"%@", [dJSON objectForKey:@"Response_Msg"]);
NSLog(@"%@", [dJSON valueForKey:@"Response_Msg"]);
NSLog(@"%@", [dJSON valueForKey:@"Response"]);
NSLog(@"%@", [dJSON objectForKey:@"Response"]);
我打印/记录的JSON结果如下所示:

响应指令={ 结果=1; “Result_Msg”=“已成功添加用户。请验证您的邮件地址。”;}

不幸的是,每个NSLog以及变量“b”和“string”都等于(null)

此外,我对这两种不同类型的键有点困惑,比如Result不带引号(“”),但Result\u Msg带引号

希望有人能帮忙。提前谢谢

编辑:

变量“dJSON”已经是NSDictionary(/MutableDic)。我是这样做的:

NSError *errorJson=nil;
    NSMutableDictionary* dJSON = [NSJSONSerialization JSONObjectWithData:_responseData options:kNilOptions error:&errorJson];
if (error)
{
    UIAlertView *alert ....
    return;
}

//---分析和提取值---/

从PHP代码中检索字典有两种简单的方法

首先,您不必在objective-c代码中更改对服务器的调用,而是使用以下函数:

- (NSDictionary *)dictionaryFromJSONData:(NSData *)data
{
    NSError *error;
    NSDictionary *dictionaryParsed = [NSJSONSerialization JSONObjectWithData:data
                                                                 options:0
                                                                   error:&error];
    if (!dictionaryParsed)
    {
        if (error)
        {
            NSLog(@"Error parsing dictionary");
        }
        return nil;
    }
    return dictionaryParsed;
}
第二是使用图书馆,顺便说一句,图书馆提供了很多有用的东西。(查看您的案例中的
AFHTTPRequestOperation

编辑

然后,您可以通过以下方式检索bollean:

BOOL myBool = [[myDictionary valueForKey:@"myKey"] boolValue];
以及您的字符串:

NSString *myString = [myDictionary objectForKey:@"myKey"];
我解决了。。。 这是我犯过的最愚蠢、最浪费时间的错误。。。为了从字典中提取值,我使用了错误的键!!!这不是回应和回应,而是结果和结果。。。。乍一看是一样的


谢谢大家

这真的是由PHP:JSON_encode()生成的JSON吗?因为PHP的JSON要求所有键和值都用引号封装。而且,仅此一点,“最后”是可疑的。无论如何,作为第一步,试着将JSON编辑成以下格式:{“Result”=“1”;“Result_Msg”=“foo”}谢谢你的回答,但我像你说的那样更改了PHP代码,但在客户端,我得到了与之前相同的响应:responseDict={Result=1;“Result_Msg”=“Success!!!”;}然后有什么东西正在破坏你的JSON字符串。很抱歉,我不能再帮你了,因为objective-c不是我的专业。我解决了它,但谢谢你的帮助!对不起,我以前忘了提那件事。。当然我以前做过转换。所以变量“dJSON”已经是一个NSDictionary了!当在代码中的这些行添加断点时,您在字典中看到了正确的值吗?是的,有常见的键/值对,如key:Result-value:1;关键词:结果信息值:成功!!!试试我在帖子中所做的编辑。你不应该用%@记录bool,但是字符串看起来很奇怪。。。此外,打印结果时不带“字符”,因为它是一个没有空格的字符串