0

以下の例のように、JSON オブジェクトのパラメーターを作成したいと思います。私は iOS 5 デバイスで作業したことがあり、NSJSONSerialization API を使用してこれを実現できました。たとえば、汎用関数「makeJSONObject()」を作成し、それを使用しています。

Sample Payload 1:
{
  token: "kjsdfjl23kkj23kk"
  entries: [
    {
      "title": "welcome",
      "name": "myself",
      "date": "2012-02-06T00:14:20Z",
    },{
      "title": "Hi",
      "name": "martin",
      "date": "2012-02-06T00:14:20Z",
    }
  ]
}

Sample Payload 2:
{
  "email" : "me@company.com",
  "password" : "pswrd"
}

コード:

NSString *jsonRequest = [appDelegate makeJSONObject:[NSArray arrayWithObjects: emailStr, passwordStr, nil] :[NSArray arrayWithObjects: @"email", @"password", nil] ];

-(NSString *) makeJSONObject :(NSArray *)objects :(NSArray *)keys
{
    NSString *theBodyString = NULL;

    NSDictionary *data = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

    //NSLog(@"data: %@", data);

    NSError *writeError = nil;    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:data options:NSJSONWritingPrettyPrinted error:&writeError];


    theBodyString = [[NSString alloc] initWithData:jsonData encoding:NSASCIIStringEncoding];

    return theBodyString;
}

しかし、今すぐ 4.0 デバイスをサポートしたいのですが、現在 NSJSONSerialization API を使用できません。私はSBJsonまたはそのようなものを使用する必要があるかもしれませんが、私にはわかりません。上記のジェネリック関数を変更して、SBJsonまたはサードパーティのパーサークラスを使用するにはどうすればよいですか?

助けてください!ありがとうございました。

4

2 に答える 2

0

NSString * jsonRequest = [NSString stringWithFormat:@ "&json_data =%@"、[[NSString stringWithFormat:@ "{\" listInvoice \ ":{\" client_id \ ":\" \ "、\" date_from \ ":\" \ "、\" date_to \ ":\" \ "、\" invoice_number \ ":\" \ "、\" invoice_record_status \ ":\" \ "、\" invoice_status \ ":\" \ "、\" page \ ":\" 1 \ "、\" per_page_record \ ":\" 20 \ "}}"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

于 2012-04-30T07:37:38.477 に答える
0

これらの基本的なデータ型からJSON文字列を構築するには、NSArray/NSDictionaryの拡張機能を試してください。 https://github.com/H2CO3/CarbonateJSON

于 2012-04-30T07:40:12.340 に答える