gpt4 book ai didi

objective-c - 解析 JSON 时出错

转载 作者:行者123 更新时间:2023-11-28 20:42:17 25 4
gpt4 key购买 nike

晚上好!

我需要一些帮助来解析来自 Google Directions 的 JSON。我有以下代码来获取 html_instructions 值:

NSString *myRawJson = [[NSString alloc] initWithContentsOfURL:
[NSURL URLWithString:@"http://maps.google.com/maps/api/directions/json?origin=-15.802737,-47.87963&destination=-15.851783,-47.954593&sensor=true"]];

// Create a dictionary from the JSON string
NSDictionary *results = [myRawJson JSONValue];

NSArray *resultarr = [results objectForKey:@"routes"];
NSString *string;
//NSArray *subarray;

for(NSDictionary *di in resultarr){
NSLog(@"loop");
NSDictionary *subdic = [[di objectForKey:@"legs"] objectForKey:@"steps"];
string = [subdic objectForKey:@"html_instructions"];
NSLog(@"%@",string);
}
(...)

当我运行它时,出现以下错误

-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x6d9b3e0 2011-11-02 22:53:02.110 APSplitSample[1470:11603] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x6d9b3e0'

这是 JSON(可以看到完整运行上面的 url):

{
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : -15.79760,
"lng" : -47.879710
},
"southwest" : {
"lat" : -15.874160,
"lng" : -47.958310
}
},
"copyrights" : "Dados cartográficos ©2011 MapLink",
"legs" : [
{
"distance" : {
"text" : "16,0 km",
"value" : 15989
},
"duration" : {
"text" : "16 minutos",
"value" : 945
},
"end_address" : "Estr. Epia - Candangolândia, Brasília - DF, Brasil",
"end_location" : {
"lat" : -15.851770,
"lng" : -47.95470
},
"start_address" : "Via de Ligação Se/ne - Brasília, DF, Brasil",
"start_location" : {
"lat" : -15.802960,
"lng" : -47.879710
},
"steps" : [
{
"distance" : {
"text" : "0,6 km",
"value" : 625
},
"duration" : {
"text" : "1 min",
"value" : 59
},
"end_location" : {
"lat" : -15.800940,
"lng" : -47.885150
},
"html_instructions" : "Siga na direção \u003cb\u003eoeste\u003c/b\u003e na \u003cb\u003eVia de Ligação Se/ne\u003c/b\u003e em direção à \u003cb\u003eVia Bs S Um\u003c/b\u003e",
"polyline" : {
"points" : "nom_BdofcHa@hB[rACRId@Kf@g@|BOl@AFKb@Kd@i@~BaArCKZiA`DKVCFGLCFKX"
},
"start_location" : {
"lat" : -15.802960,
"lng" : -47.879710
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0,1 km",
"value" : 101
},
(...)

有人可以帮忙吗?!提前致谢!

最佳答案

您好,我希望使用此代码可能对您有所帮助

    NSDictionary *mainDict=[requeststring JSONValue];

//NSLog(@"maindict values is %d",[mainDict count]);


NSArray *routesArray=[mainDict objectForKey:@"routes"];

if ([routesArray count]>0)
{


NSDictionary *routeDict=[routesArray objectAtIndex:0];

NSArray *legsarray=[routeDict objectForKey:@"legs"];



NSDictionary *legsdict=[legsarray objectAtIndex:0];







NSDictionary *distDict=[legsdict objectForKey:@"distance"];

NSString *distncestring=[distDict objectForKey:@"text"];

NSDictionary *timeDict=[legsdict objectForKey:@"duration"];

NSString *time=[timeDict objectForKey:@"text"];




NSDictionary *startlatdict=[legsdict objectForKey:@"start_location"];
float latt=[[startlatdict objectForKey:@"lat"] floatValue];
float long=[[startlatdict objectForKey:@"lng"] floatValue];






NSArray *stepsarray=[legsdict objectForKey:@"steps"];

for (int i=0; i<[stepsarray count]; i++)
{

NSDictionary *stepsdict=[stepsarray objectAtIndex:i];

NSDictionary *endlattdict=[stepsdict objectForKey:@"end_location"];

float lang1=[[endlattdict objectForKey:@"lat"] floatValue];
float long1=[[endlattdict objectForKey:@"lng"] floatValue];


NSString *instStr =[stepsdict objectForKey:@"html_instructions"];

NSLog(@"html_instructions%@",instStr);




}

此代码可帮助您获取 google map json 数据的所有信息

祝你好运

关于objective-c - 解析 JSON 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7989173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com