私のJSON-RPCクエリに問題があります。結果として、私はこの出力を得るObjective-C JSON-RPCリクエストの問題
NSString *jsonString = @"{id:1,method:getHolidays,jsonrpc:2.0}";
NSString *requestString = [NSString stringWithFormat:@"%@",jsonString,nil];
NSData *requestData = [NSData dataWithBytes: [jsonString UTF8String] length: [requestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:@"https://neilo.webuntis.com/WebUntis/jsonrpc.do?school=htl_v%C3%B6cklabruck"]];
[request setHTTPMethod: @"POST"];
[request setValue:@"Content-type: application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:requestData];
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
NSLog(@"\n\n\noutput: %@",returnString);
:
{"jsonrpc":"2.0","id":null,"error":{"code":-32700,"message":"Parse error: Unexpected character ('i' (code 105)): was expecting double-quote to start field name\n at [Source: [email protected]; line: 1, column: 3]"}}
結果は次のようになります。 - >message = not-authenticated
< -
誰もが、問題が何であるか任意のアイデアを持っていますか?
'NSDictionaryの* jsonDict = @ {@ "id":@ 1、 "メソッド":@ "getHolidays"、@ "jsonrpc":@ "2.0"};あなたがJSONバリデータに文字列JSONをコピー/ペーストすると、それは有効ではないことがわかります。NSNET * jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:0エラー:&error]; '...' [setHTTPBody:jsonData] (文字列を囲む引用符がありません)。これは 'NSJSONSerialization'が辞書を使って構築されているため(パラメータを変更しやすいため)、NSJSONSerializationが処理して簡素化する' {"id":1、 "method": "getHolidays"、 "jsonrpc":2.0} 'でなければなりません。 – Larme
そうですね! NSString * jsonString = @ "{\" id \ ":\"メソッド\ ":\" getHolidays \ "、\" jsonrpc \ ":\" 2.0 \ "}"; – Darkdrummer
自分でJSON Stringを構築するのではなく、 'NSJSONSerialization'を使うことをお勧めします。より安全です(エラーパラメータが有効でない場合は、特に変更可能なNSMutableArray/Dictionaryを使用して、追加することができるキーがある場合は変更する方が簡単です)。また、 'sendSynchronousRequest:returningResponse:error:'を避け、 'NSURLSession'を使って非同期のものを優先します。 – Larme