解决方案 »

  1.   

    先将整段json转为字典,比如叫resultnsarray *arr=[reuslt objectforkey:@"list"];
    NSDictionary *dict=[arr objectatindex:0];
    NSDictionary *data=[dict objectforkey:@"data"];
    nsstring *localtion=[data valueforkey:"location"];
      

  2.   

    错了,改一下,好像data那边也是数组NSArray *arr=[reuslt ObjectForKey:@"list"];
    NSDictionary *dict=[arr ObjectAtIndex:0];
    NSArray *data=[dict ObjectForKey:@"data"];
    NSDictionary *dict2=[data ObjectAtIndex];
    NSstring *localtion=[dict2 ValueForKey:"location"];
      

  3.   

    只能是一层一层找了。/////找到list
    NSArray *list = result[@"list"];
    //////得到数组中的第一条数据
    NSDictionary *first =(NSDictionary *)list[0];
     //////找到data 数组
    NSArray *data  = first[@"data"];
    //////得到data的第一条数据
    NSDictionary *data_first = (NSDictionary *)data[0];
    ///////得到最终要找的location
    NSString *location = data_first[@"location"];
      

  4.   

    非常感谢您的回答。但还有一个问题,得到的location的值应该是两个double值35.791517和104.628896,
    我怎样得到这两个值呢?
      

  5.   

    非常感谢您的回答。但还有一个问题,得到的location的值应该是两个double值35.791517和104.628896,
    我怎样得到这两个值呢?
      

  6.   

    非常感谢您的回答。但还有一个问题,得到的location的值应该是两个double值35.791517和104.628896,
    我怎样得到这两个值呢?
    可根据,字符分割成一个数组
    NSString *location =@"35.791517,104.628896";
    NSArray *locations = [location componentsSeparatedByString:@","];
    NSLog(@"%f, %f", [locations[0] CGFloat],[locations[1] CGFloat]);
      

  7.   

    你遍历字典的目的是什么只是为了找location的值而不关心其他的值吗?
      

  8.   

    非常感谢您的回答。但还有一个问题,得到的location的值应该是两个double值35.791517和104.628896,
    我怎样得到这两个值呢?
    可根据,字符分割成一个数组
    NSString *location =@"35.791517,104.628896";
    NSArray *locations = [location componentsSeparatedByString:@","];
    NSLog(@"%f, %f", [locations[0] CGFloat],[locations[1] CGFloat]);就是这个方法
      

  9.   

    暂时只是要得到location,你一问倒让我想起怎么来全部遍历啊?
      

  10.   

    非常感谢您的回答。但还有一个问题,得到的location的值应该是两个double值35.791517和104.628896,
    我怎样得到这两个值呢?
    可根据,字符分割成一个数组
    NSString *location =@"35.791517,104.628896";
    NSArray *locations = [location componentsSeparatedByString:@","];
    NSLog(@"%f, %f", [locations[0] CGFloat],[locations[1] CGFloat]);
    很感谢,成功得到。
      

  11.   

    又遇到一个新问题,关于json http通讯的。
    我的代码如下,/*
        
         json={
         EchoToken:” 74384954334”,
         action:”history”,
         boxsn:”45632325533455664”,
         begin: ”2014/11/18”,
         end: ”2014/11/18”,
         number:”13581959521”,
         sn:”00000000000000”,
         token:”D41D8CD98F00B204E9800998ECF8427E”,
         appver:”3.01”
         }*/
        /*
        NSMutableDictionary * dicRequestPara = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                        @"EchoToken",@"74384954334",
                                        @"action",@"history",
                                        @"boxsn",@"00000000000000003",
                                        @"begin",@"",
                                        @"end",@"",
                                        @"count",@"10",
                                        @"number",@"13581959521",
                                        @"sn",@"00000000000000",
                                        @"token",@"D41D8CD98F00B204E9800998ECF8427E",
                                        @"appver",@"3.01",
                                         nil];
        */
        NSArray *value = [NSArray arrayWithObjects:@"74384954334",@"history",@"00000000000000003",@"",@"",@"1",@"13581959521",@"00000000000000",@"D41D8CD98F00B204E9800998ECF8427E",@"3.01", nil];
        NSArray * key = [NSArray arrayWithObjects:@"EchoToken",@"action", @"boxsn",@"begin",@"end",@"count",@"number",@"sn",@"token",@"appver",nil];
        NSDictionary * dicRequestPara = [NSDictionary dictionaryWithObjects:value forKeys:key];
        /*NSDictionary * dicRequestPara = [NSDictionary dictionaryWithObjectsAndKeys:
                                                @"74384954334",@"EchoToken",
                                                @"history",@"action",
                                                @"00000000000000003",@"boxsn",
                                                @"",@"begin",
                                                @"",@"end",
                                                @"10",@"count",
                                                @"13581959521",@"number",
                                                @"00000000000000",@"sn",
                                                @"D41D8CD98F00B204E9800998ECF8427E",@"token",
                                                @"3.01",@"appver",
                                                nil];
        */
        //[dicRequestPara setObject:@"" forKey:@"begin"];
        
        NSData * jsonData;
        NSString *jsonStr;
        NSError *error = nil;
        if ([NSJSONSerialization isValidJSONObject:dicRequestPara])
        {
            
            jsonData = [NSJSONSerialization dataWithJSONObject:dicRequestPara options:NSJSONWritingPrettyPrinted error:&error];
            if ([jsonData length]>0 && error == nil) {
                jsonStr =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
                
                NSLog(@"json data:%@",jsonStr);
                //NSMutableData *requestData = [[NSMutableData alloc] init];
                // [requestData appendData:[jsonStr dataUsingEncoding:NSUTF8StringEncoding]];
                
                [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
                [request addValue:[NSString stringWithFormat:@"%d",[jsonData length]] forHTTPHeaderField:@"Content-Length"];
                [request setHTTPMethod:@"POST"];
                [request setHTTPBody:jsonData];
                
                //第三步,连接服务器
                self.connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
            }
            else
            {
                NSLog(@"dataWithJSONObject no");
            }
        }
        else
        {
            NSLog(@"isValidJSONObject no.");
        }返回的json数据格式应该是:
    {
    EchoToken:” 74384954334”,
    result: 0,
    list:[
    {boxsn:” 45632325533455664”, data:[
    {lasttime: “2014/12/10 20:35:25”, power:”27”, latlng:”40.21223, -120.7512”, dir:”1,1”}, {lasttime: “2014/12/10 20:35:40”, power:”27”, latlng:”40.21231, -120.7518”, dir:”3,1”},
    ..
    {lasttime: “2014/12/10 22:03:15”, power:”18”, latlng:”40.24182, -120.7169”, dir:”1,2”}
    ]
    }],
    Timestamp: 1412947470
    }
    现在的问题是:当我字典初始化dictionaryWithObjectsAndKeys时,如果参数是 key1,value1,key2,value2,...
    时(其实这是不正确的,正确初始化是先value后key的。)返回的数据是:(list是要得到的主要数据)
    {
        EchoToken = "<null>";    为空
        Timestamp = "2014/12/3 18:31:23";
        list =     (      有值
                    {
                boxsn = 00000000000003;
                data =             (
                                    {
                        dir = "1,1";
                        lasttime = "2014/12/03 07:55:42";
                        latlng = "39.985472,116.430809";
                        msg = "<null>";
                        power = 100;
                    }
                );
            }
        );
        msg = "<null>";
        result = 0;
    }
    当已先value后key初始化时,返回的数据是:
    {
        EchoToken = 74384954334;    有值
        Timestamp = "2014/12/3 18:22:46"; 
        list = "<null>";  为空
        msg = "<null>"; 
        result = 0;
    }
    为什么会出现这种情况?是我写的问题,还是服务器解析的问题?多谢了。若解决了,必定重谢
      

  12.   

    又遇到一个新问题,关于json http通讯的。
    我的代码如下,/*
        
         json={
         EchoToken:” 74384954334”,
         action:”history”,
         boxsn:”45632325533455664”,
         begin: ”2014/11/18”,
         end: ”2014/11/18”,
         number:”13581959521”,
         sn:”00000000000000”,
         token:”D41D8CD98F00B204E9800998ECF8427E”,
         appver:”3.01”
         }*/
        /*
        NSMutableDictionary * dicRequestPara = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                        @"EchoToken",@"74384954334",
                                        @"action",@"history",
                                        @"boxsn",@"00000000000000003",
                                        @"begin",@"",
                                        @"end",@"",
                                        @"count",@"10",
                                        @"number",@"13581959521",
                                        @"sn",@"00000000000000",
                                        @"token",@"D41D8CD98F00B204E9800998ECF8427E",
                                        @"appver",@"3.01",
                                         nil];
        */
        NSArray *value = [NSArray arrayWithObjects:@"74384954334",@"history",@"00000000000000003",@"",@"",@"1",@"13581959521",@"00000000000000",@"D41D8CD98F00B204E9800998ECF8427E",@"3.01", nil];
        NSArray * key = [NSArray arrayWithObjects:@"EchoToken",@"action", @"boxsn",@"begin",@"end",@"count",@"number",@"sn",@"token",@"appver",nil];
        NSDictionary * dicRequestPara = [NSDictionary dictionaryWithObjects:value forKeys:key];
        /*NSDictionary * dicRequestPara = [NSDictionary dictionaryWithObjectsAndKeys:
                                                @"74384954334",@"EchoToken",
                                                @"history",@"action",
                                                @"00000000000000003",@"boxsn",
                                                @"",@"begin",
                                                @"",@"end",
                                                @"10",@"count",
                                                @"13581959521",@"number",
                                                @"00000000000000",@"sn",
                                                @"D41D8CD98F00B204E9800998ECF8427E",@"token",
                                                @"3.01",@"appver",
                                                nil];
        */
        //[dicRequestPara setObject:@"" forKey:@"begin"];
        
        NSData * jsonData;
        NSString *jsonStr;
        NSError *error = nil;
        if ([NSJSONSerialization isValidJSONObject:dicRequestPara])
        {
            
            jsonData = [NSJSONSerialization dataWithJSONObject:dicRequestPara options:NSJSONWritingPrettyPrinted error:&error];
            if ([jsonData length]>0 && error == nil) {
                jsonStr =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
                
                NSLog(@"json data:%@",jsonStr);
                //NSMutableData *requestData = [[NSMutableData alloc] init];
                // [requestData appendData:[jsonStr dataUsingEncoding:NSUTF8StringEncoding]];
                
                [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
                [request addValue:[NSString stringWithFormat:@"%d",[jsonData length]] forHTTPHeaderField:@"Content-Length"];
                [request setHTTPMethod:@"POST"];
                [request setHTTPBody:jsonData];
                
                //第三步,连接服务器
                self.connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
            }
            else
            {
                NSLog(@"dataWithJSONObject no");
            }
        }
        else
        {
            NSLog(@"isValidJSONObject no.");
        }返回的json数据格式应该是:
    {
    EchoToken:” 74384954334”,
    result: 0,
    list:[
    {boxsn:” 45632325533455664”, data:[
    {lasttime: “2014/12/10 20:35:25”, power:”27”, latlng:”40.21223, -120.7512”, dir:”1,1”}, {lasttime: “2014/12/10 20:35:40”, power:”27”, latlng:”40.21231, -120.7518”, dir:”3,1”},
    ..
    {lasttime: “2014/12/10 22:03:15”, power:”18”, latlng:”40.24182, -120.7169”, dir:”1,2”}
    ]
    }],
    Timestamp: 1412947470
    }
    现在的问题是:当我字典初始化dictionaryWithObjectsAndKeys时,如果参数是 key1,value1,key2,value2,...
    时(其实这是不正确的,正确初始化是先value后key的。)返回的数据是:(list是要得到的主要数据)
    {
        EchoToken = "<null>";    为空
        Timestamp = "2014/12/3 18:31:23";
        list =     (      有值
                    {
                boxsn = 00000000000003;
                data =             (
                                    {
                        dir = "1,1";
                        lasttime = "2014/12/03 07:55:42";
                        latlng = "39.985472,116.430809";
                        msg = "<null>";
                        power = 100;
                    }
                );
            }
        );
        msg = "<null>";
        result = 0;
    }
    当已先value后key初始化时,返回的数据是:
    {
        EchoToken = 74384954334;    有值
        Timestamp = "2014/12/3 18:22:46"; 
        list = "<null>";  为空
        msg = "<null>"; 
        result = 0;
    }
    为什么会出现这种情况?是我写的问题,还是服务器解析的问题?多谢了。若解决了,必定重谢在使用dictionaryWithObjectsAndKeys 创建NSDictionary 对象时一定不能搞错顺序,不然方向是反的,value 的值变成了key,那么在服务器端取参数的值时也就会出问题。
    创建NSDictionary对象时,建议可以使用如下再易读的方式
    NSDictionary * dicRequestPara =@{@"EchoToken":@"74384954334",@"action":@"history",@"boxsn":@"000000",@"begin":@"",@"end":@""};你这里不好说是谁的问题,你只能在服务器端断点调试服务器端的接口程序,看接收到的参数有没有问题。在处理返回后,是不是你想要的结果。所以我的建议是你重点检查一下服务器端,在服务器端调试,这样即能知道客户端传过来的参数是否有问题,也能知道服务器端处理的是否也正常
      

  13.   

        NSArray *value = [NSArray arrayWithObjects:@"74384954334",@"history",@"00000000000000003",@"",@"",@"1",@"13581959521",@"00000000000000",@"D41D8CD98F00B204E9800998ECF8427E",@"3.01", nil];
        NSArray * key = [NSArray arrayWithObjects:@"EchoToken",@"action", @"boxsn",@"begin",@"end",@"count",@"number",@"sn",@"token",@"appver",nil];
        NSDictionary * dicRequestPara = [NSDictionary dictionaryWithObjects:value forKeys:key];这么写的话有可能你创建出来的字典dicRequestPara应该只有一个键值对
    就是 数组key=数组value
    建议用楼上说的方法来组织字典
    NSDictionary * dicRequestPara =@{@"EchoToken":@"74384954334",@"action":@"history",@"boxsn":@"000000",@"begin":@"",@"end":@""};
      

  14.   

    问题已经解决,是因为 boxsn的值位数不对造成的。字典初始化的键和值位置对调后反而会得到数据可能是因为服务器端程序比较boxsn的值,没有相等的就返回了最后一个boxsn的数据,正好是我要得到的boxsn的数据,所以出现这样的情况。非常感谢大家的帮助。