1

私は辞書にいくつかのデータを持っています

 NSMutableDictionary *jsonDictionary;
 jsonDictionary = [[NSMutableDictionary alloc] init];
 [jsonDictionary setValue:@"XYZ" forKey:@"CommandType"];
 [jsonDictionary setValue:@"ABC" forKey:@"AppID"];
 [jsonDictionary setValue:@"PQM" forKey:@"UserName"];

 NSURLResponse *response;
 NSError *error;

 NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:NSJSONWritingPrettyPrinted error:&error];

 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:
                                [NSURL URLWithString:@"Http://SomeURL"]];

[request setHTTPMethod:@"POST"];
[request setHTTPBody: jsonData];

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *serverResponse = (NSString *)[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

NSMutableDictionary *results = [serverResponse JSONValue];

私の「結果」では、私はnullになっています..何が問題なのか、コンソールにエラーが表示されています。エラーは以下の通りです。

 -JSONRepresentation failed. Error trace is: (
"Error Domain=org.brautaset.JSON.ErrorDomain Code=4 \"Not valid type for JSON\" UserInfo=0x6b8d880 {NSLocalizedDescription=Not valid type for JSON}"

 -JSONFragment failed. Error trace is: (
"Error Domain=org.brautaset.JSON.ErrorDomain Code=1 \"JSON serialisation not supported for NSMutableURLRequest\" UserInfo=0x6b8ddf0 {NSLocalizedDescription=JSON serialisation not supported for NSMutableURLRequest}"

この問題を解決するために私を助けることができます。

4

3 に答える 3

2

NSJSONSerializationを使用して、JSON への変換を実行してみてください。

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
id result = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];

これが機能しない場合は、サーバーが空の応答を返していることが原因であると確信しています。次のリクエストを送信してみました。

Accept: / Content-Type: application/json Content-Length: 65 Accept-Language: en-us Accept-Encoding: gzip, deflate

{ "AppID": "ABC", "CommandType": "XYZ", "UserName": "PQM" }

そして、次の応答ヘッダーが返されました(本文なし):

HTTP/1.1 200 OK

日付: 2013 年 1 月 30 日水曜日 14:58:43 GMT X-AspNet-Version: 4.0.30319 Content-Length: 0 X-Powered-By: ASP.NET Cache-Control: プライベート サーバー: Microsoft-IIS/7.5

コンテンツの長さは 0 です。

于 2013-01-30T13:48:17.893 に答える
0

この行

[request setHTTPBody: temp];

する必要があります

[request setHTTPBody: jsonData];
于 2013-01-30T13:49:03.227 に答える
-1

私はあなたに役立つと思います。あなたはこれを次のCode.NSJSONシリアル化メソッドで試すことができます。そしてこのリンクを試すことができますNSJSONシリアル化JSON

NSError *error = nil;
NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://192.168.1.37:3333/UniECommerce/api/store-list.html"]];

    if (jsonData) 
    {
        id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];

        if (error) 
        {
            NSLog(@"error is %@", [error localizedDescription]);

            // Handle Error and return
            return;
        }

        //NSArray *keys = [jsonObjects allKeys];
        NSArray *keys=[[jsonObjects objectForKey:@"storelist"]valueForKey:@"store_list"];
        // values in foreach loop
        for (NSDictionary *key in keys)// here used to array type key 
        {
            // NSLog(@"%@ is %@",key, [jsonObjects objectForKey:key]);

            if ([key valueForKey:@"store_image"]!=[NSNull null])
            {

              [imageUrls  addObject:[key objectForKey:@"store_image"]];
               // imageUrls [key valueForKey:@"store_image"];
            }
        }
于 2013-01-30T13:53:14.073 に答える