0

JSON の解析は初めてで、天気予報の json ファイルから URL を取得するという単純なタスクを試みています。

ここで、json を解析し、データの各コンポーネントの内容を NSLog します。

NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves  error:&myError];

NSArray *data =  [res objectForKey:@"data"];
NSLog(@"data=%@",data);

NSArray *results =  [data valueForKey:@"weather"];
NSLog(@"weather=%@",results);

NSArray *results1 =  [results valueForKey:@"tempMaxC"];
NSLog(@"tempMaxC=%@",results1);

NSArray *results2 =  [results1 valueForKey:@"weatherIconUrl"];
NSLog(@"weatherIconUrl=%@",results2);

問題は、WeatherIconUrl を取得すると、この形式が付属していることです。

"http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"

引用符なしでURL自体を取得することはできません.nsrangeとcomponentsSeparatedByStringを使用しようとしましたが、常にこのエラーが発生します:

[__NSArrayI componentsSeparatedByString:]: unrecognized selector sent to instance 

サーバーからの JSON:

{
    "data": {
        "current_condition": [
            {
                "cloudcover": "0",
                "humidity": "73",
                "observation_time": "12:19 PM",
                "precipMM": "0.0",
                "pressure": "1021",
                "temp_C": "23",
                "temp_F": "73",
                "visibility": "10",
                "weatherCode": "113",
                "weatherDesc": [
                    {
                        "value": "Sunny"
                    }
                ],
                "weatherIconUrl": [
                    {
                        "value": "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
                    }
                ],
                "winddir16Point": "NW",
                "winddirDegree": "320",
                "windspeedKmph": "17",
                "windspeedMiles": "11"
            }
        ],
        "request": [
            {
                "query": "Fanzeres, Portugal",
                "type": "City"
            }
        ],
        "weather": [
            {
                "date": "2012-09-12",
                "precipMM": "0.0",
                "tempMaxC": "28",
                "tempMaxF": "83",
                "tempMinC": "17",
                "tempMinF": "63",
                "weatherCode": "113",
                "weatherDesc": [
                    {
                        "value": "Sunny"
                    }
                ],
                "weatherIconUrl": [
                    {
                        "value": "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
                    }
                ],
                "winddir16Point": "NW",
                "winddirDegree": "312",
                "winddirection": "NW",
                "windspeedKmph": "16",
                "windspeedMiles": "10"
            },
            {
                "date": "2012-09-13",
                "precipMM": "0.0",
                "tempMaxC": "33",
                "tempMaxF": "91",
                "tempMinC": "17",
                "tempMinF": "63",
                "weatherCode": "113",
                "weatherDesc": [
                    {
                        "value": "Sunny"
                    }
                ],
                "weatherIconUrl": [
                    {
                        "value": "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
                    }
                ],
                "winddir16Point": "N",
                "winddirDegree": "8",
                "winddirection": "N",
                "windspeedKmph": "10",
                "windspeedMiles": "6"
            }
        ]
    }
}

英語が下手で申し訳ありませんが、これが間違っている場合は修正してください。事前に感謝します

4

3 に答える 3

3

@"weatherIconUrl" から配列を取得するときに valueForKey の代わりに objectForKey を使用し、文字列を NSString に取得します。

NSString *weatherIconUrlString = [results2 objectAtIndex:0]

これが有効な URL であることを確認するには、NSURLConnection の canHandleRequest メソッドを使用します。

NSURL *url = [NSURL URLWithString:weatherIconUrlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url 
BOOL canGo = [NSURLConnection canHandleRequest:request];
于 2012-09-12T09:49:09.367 に答える
2

URL を本当に引用符で囲んでいる場合は、次のようにしてみてください。

NSString *someURLString = [results2 objectAtIndex:0];
NSString *quotesRemoved = [someURLString stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\""]];
于 2012-09-12T09:48:25.600 に答える
1

サーバーの出力をjsonLint.comに渡すと、json の形式が読みやすくなります。

以下のコードは、必要に応じて天気アイコンの URL を取得します。json が jsonData と呼ばれる NSData オブジェクトとしてダウンロードされていると想定し、データが参照する日付をチェックしません。

NSError *error = nil;
NSDictionary *jsonDict      = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves
                                                                error:&error];
NSArray      *data          =  [jsonDict    valueForKey:@"data"];
NSArray      *weather       =  [data        valueForKey:@"weather"];
NSArray      *weatherIcon   = [[weather     objectAtIndex:0] valueForKey:@"weatherIconUrl"];
NSString     *url           = [[weatherIcon objectAtIndex:0] valueForKey:@"value"];

結果の URL は NSURLRequest で使用され、webview に表示されます

ここに画像の説明を入力

于 2012-09-12T11:28:29.127 に答える