5

json 文字列を動的に生成し、サーバーに送信する必要があります。を使用してそれを行う方法を知っている体はありますかNSJSONSerialization。以下は私の文字列です

{
    "surveyid": "Survey1",
    "responsetime": "dd/mm/yyyy hh:mm:ss",
    "location": null,
    "surveyresponses": [
        {
            "questionid": "111",
            "responses": [
                {
                    "response": "Good",
                    "optionid": 1,
                    "language": "en"
                }
            ]
        },
        {
            "questionid": "112",
            "responses": [
                {
                    "response": "bad",
                    "optionid": 2,
                    "language": "en"
                }
            ]
        }

    ]
}

string.json を作成するにはどうすればよいですか?

4

8 に答える 8

3

私は@Apurvに完全に同意します。実際には@Paras Joshiがあなたの質問に実際の回答を提供します...(送信方法)

NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setValue:@"Survey1" forKey:@"surveyid"];
[dict setValue:@"responsetime" forKey:@"dd/mm/yyyy hh:mm:ss"];
.
.
.

そして配列forKey @"surveyresponses"...その中に再び辞書を作成します... bla..bla

最初に、正確に何を望んでいるのかを明確にしてください... JSON文字列を送信する方法/ JSON値を生成する方法。

答えを受け入れることと、答えを完璧に分析することを学び、質問を明確にします。

于 2013-03-20T07:35:18.660 に答える
3

以下のように、JSON オブジェクトを使用して Data の辞書を設定します。

NSError *err;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:yourDataDictionary options:NSJSONWritingPrettyPrinted error:&err];

NSLog(@"JSON = %@", [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);

このリンクも参照してくださいsend-string-as-json-object

于 2013-03-20T06:48:54.773 に答える
1

すでにJSON文字列がある場合は、直接使用できます。

オブジェクト(配列と辞書)があり、それをjson文字列に変換する場合は、以下のメソッドを使用しますNSJSONSerialization

NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setValue:@"Survey1" forKey:@"surveyid"];
//Add rest of the details in the dictionary

//Pass the dict in below method
    + (NSData *)dataWithJSONObject:(id)dict options:(NSJSONWritingOptions)opt error:(NSError **)error

次に、上記のデータを使用して文字列オブジェクトを作成します。

于 2013-03-20T06:42:28.417 に答える
1

Json文字列を生成するためのサンプルコード:

 NSString *strdata =[NSString stringWithFormat:@"&data={\"user_id\":\"%@\",\"auth_token\":\"%@\",\"coupon_id\":\"%@\",\"rest_name\":\"%@\",\"name\":\"%@\",\"description\":\"%@\",\"condition\":\"%@\",\"coupon_code\":\"%@\",\"parent_menu_item\":\"%@\",\"discount_item\":\"%@\",\"discount_pecent\":\"%@\",\"start_date\":\"%@\",\"end_date\":\"%@\",\"status\":\"%@\"}",userid,auth_token,couponid,restuserid,txtCoupannm.text,txtvwCoupandesc.text,txtvwcoupancond.text,couponcode,appDelegate.tempparentmenuId,appDelegate.tempdiscountitemId,txtPercent.text,startdate.text,enddate.text,@"1"];
于 2013-03-20T06:58:56.113 に答える
1

これを試してください:複数のアイテムがある場合は、 (str = ) をループに入れます

    NSString *str = [NSString stringWithFormat:@"json_data={\"data\":["];

    str = [str stringByAppendingString:[NSString stringWithFormat:@"{\"type\":\"%@\",\"surveyid\":\"%@\",\"veriety\":\"%@\",\"responsetime\":\"%@\",\"rate\":\"%@\",\"seeddepth\":\"%@\",\"groundspeed\":\"%@\",\"note\":\"%@\",\"AnhRate\":\"%@\",\"AnhVarRate\":\"%@\",\"WetRate\":\"%@\",\"WetVarRate\":\"%@\",\"WetType\":\"%@\",\"DryRate\":\"%@\",\"DryVarRate\":\"%@\",\"DryType\":\"%@\",\"MicroRate\":\"%@\",\"MicroVarRate\":\"%@\",\"MicroType\":\"%@\",\"NoteApp\":\"%@\",\"userid\":\"%@\",\"flid\":\"%d\",\"catid\":\"%d\",\"subcatId\":\"%d\",\"categoryname\":\"%@\",\"subcategoryname\":\"%@\",\"activitydate\":\"%@\",\"DateCreated\":\"%@\",\"DateUpdated\":\"%@\"},",Your variable

    if ([str length] > 0) {
            str =[str substringToIndex:[str length] - 1];
    }

str = [str stringByAppendingString:@"]}"];
于 2013-03-20T07:27:42.503 に答える
1

私は似たようなものを構築し、私が行ったことをあなたの文字列に適用する必要がありました.結果を達成するための最良の方法ではないかもしれませんが、私にとってはうまくいきました. 内側の調査回答配列 (私の場合は別の配列) については、単純にいくつかを連結しました。

NSString *ts=@"2015-04-03T13:00:00";
    NSString *outer = [NSString stringWithFormat: @"{\"surveyId\":\"Survey1\", \"responsetime\":\"%@\", \"location\":\"%@\", \"surveyresponses\":[{\"", ts, @"null"];


    NSString * surveyresponse = [NSString stringWithFormat: @"questionid\":\"111\", \"responses\":[{\"response\":\"good\", \"optionid\":\%d\, \"language\":\"en\"}]", 1];

    NSString *ending = @"}]}";

    NSString *jsonStr = [NSString stringWithFormat:@"%@%@%@", outer, surveyresponse, ending];

    NSData * postdata = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];

    NSError * error = nil;
    id json = [NSJSONSerialization JSONObjectWithData:postdata options:0 error:&error];

    if (!json) {
        // handle error
        NSLog(@"json string error - %@", error.description);
    }
    NSLog(@"%@",[[NSString alloc] initWithData:postdata encoding:NSUTF8StringEncoding]);
于 2015-09-21T13:40:25.910 に答える
0
NSDictionary* jsonResp = [NSJSONSerialization JSONObjectWithData:respData options:kNilOptions error:&error];
于 2013-03-20T09:57:22.113 に答える