ios 如何在objective-c中创建json

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11932864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:43:08  来源:igfitidea点击:

how to create json in objective-c

iosobjective-cjsonnsjsonserialization

提问by meth

NSData* jsonDataToSendTheServer;

NSDictionary *setUser = [NSDictionary
            dictionaryWithObjectsAndKeys:[@"u" stringByAppendingString:my.id],@"id",
                                        @"GET_USER_INFO",@"command",
                                        @"",@"value",
                                        nil];

 NSLog(@"%@", jsonDataToSendTheServer);

Here is my code. When I run above code I get this print

这是我的代码。当我运行上面的代码时,我得到了这个打印

<7b226964 223a2275 35383738 37373334 31222c22 636f6d6d 616e6422 3a224745 545f5553 45525f49 4e464f22 2c227661 6c756522 3a22227d>

I don't have any idea whether I can created a json or not.

我不知道我是否可以创建一个 json。

How can I fix this?

我怎样才能解决这个问题?

回答by bryanmac

You're missing this line to convert it to json

您缺少将其转换为 json 的这一行

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

Here's a tutorial on NSJSONSerialization that may help you: http://www.raywenderlich.com/5492/working-with-json-in-ios-5

这是一个关于 NSJSONSerialization 的教程,可以帮助你:http://www.raywenderlich.com/5492/working-with-json-in-ios-5

After that, you can convert the NSData to an NSString to print:

之后,您可以将 NSData 转换为 NSString 进行打印:

Convert UTF-8 encoded NSData to NSString

将 UTF-8 编码的 NSData 转换为 NSString

回答by V-Xtreme

You can try following to create JSON:

您可以尝试按照以下步骤创建 JSON:

NSArray *objects=[[NSArray alloc]initWithObjects:objects here,nil];
NSArray *keys=[[NSArray alloc]initWithObjects:corresponding keys of objects,nil];
NSDictionary *dict=[NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSData *jsonData=[NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];

this worked perfectly in my case

这在我的情况下非常有效

回答by Hardik Mamtora

Try below

试试下面

NSDictionary *o1 = [NSDictionary dictionaryWithObjectsAndKeys:
@"ABCD", @"key1",
@"EFG", @"key2",
nil];

NSDictionary *o2 = [NSDictionary dictionaryWithObjectsAndKeys:
@"XYZ", @"key1",
@"POI", @"key2",
nil];

NSArray *array = [NSArray arrayWithObjects:o1, o2, nil];

NSString *jsonString = [array JSONRepresentation];

// send jsonString to the server After executing the code above, jsonString contains:

// 向服务器发送 jsonString 执行上述代码后,jsonString 包含:

[
    {
        "key1": "ABCD",
        "key2": "EFG"
    },
    {
        "key1": "XYZ",
        "key2": "POI"
    }
]

回答by Matheus Rohwedder

Try this

尝试这个

NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://api.iospond.com/api/index.php/GetData"]];
    NSError *error=nil;
    id response=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    NSLog(@"Your JSON Object: %@ Or Error is: %@", response, error);

回答by Oshitha Wimalasuriya

NSDictionary *jsonObject = @{
                                 @"a":@[
                @{
                                             @"title1”:@“AA”,
                                             @"title2” : @“BB”,
                                             @"subcats" : @[
                                                     @{
                                                         @"title1” : @“CC”,
                                                         @"title2” :@“DD”
                                                         }

                                                     ]

                                             }
                                         ]



                                 };

回答by Arjun

NSMutableString *mutableString = nil; NSString *string= @"";

NSMutableString *mutableString = nil; NSString *string= @"";

@try
{
    if (mutableString == nil)
    {
        mutableString = [[NSMutableString alloc] init];
    }

    [mutableString appendFormat:@"{"];
    [mutableString appendFormat:@"\"string1\":%@"",",@""];
    [mutableString appendFormat:@"\"string2\":\"%@\"",@""];
    [mutableString appendFormat:@"}"];
    jsonString = mutableString ;
}
@catch (NSException *exception)
{

}
@finally
{
    return string;
}