iPhone:JSON 消息未正确创建

Posted

技术标签:

【中文标题】iPhone:JSON 消息未正确创建【英文标题】:iPhone : JSON message not creating correctly 【发布时间】:2012-02-28 14:37:22 【问题描述】:

我使用了以下代码:

    [dict setObject:[NSString stringWithFormat:@"%d",1] forKey:@"res_id"];


    //[dict setObject:categoryId forKey:@"category"];
    NSArray * values = [cartDict allValues];

    NSString *request1 = [dict JSONRepresentation];
    NSLog(request1);
    NSDictionary *req = [NSDictionary dictionaryWithObject:dict forKey:@""];

    //convert object to data
    NSData *jsonData = [NSData dataWithBytes:[request1 UTF8String] length:[request1 length]];


    //print out the data contents
    NSString *json1 = [[NSString alloc] initWithData:jsonData
                                             encoding:NSUTF8StringEncoding];

    NSLog(@"JSON>>>>>%@",json1);

【问题讨论】:

【参考方案1】:

老兄,你的代码太乱了 试试这个简单的例子,希望你能明白。它正在打印您需要的相同内容

NSMutableDictionary *cartDict = [[NSMutableDictionary alloc] init];
[cartDict setObject:[NSString stringWithFormat:@"%@",@"tId"] forKey:@"id"];
[cartDict setObject:[NSString stringWithFormat:@"%@",@"tname"] forKey:@"menuname"];    
[cartDict setObject:[NSString stringWithFormat:@"%@",@"tprice"] forKey:@"price"];
[cartDict setObject:[NSString stringWithFormat:@"%@",@"tquan"] forKey:@"quantity"];
NSArray *array = [NSArray arrayWithObject:cartDict];        
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:[NSString stringWithFormat:@"%@",@"personName"] forKey:@"customerName"];
[dict setObject:[NSString stringWithFormat:@"%@",@"emailid"] forKey:@"cusmailaddress"];
[dict setObject:[NSString stringWithFormat:@"%d",1] forKey:@"res_id"];
[dict setObject:array forKey:@"Order Details"];
NSLog(@"%@",dict);

【讨论】:

【参考方案2】:

而不是这个

  NSDictionary* info=[NSDictionary dictionaryWithObject:cartDict forKey:@"Order Details"];

试试这个。 将您的“cartDict”字典放入数组中,然后将该数组再次添加到字典中以实现您的 输出

【讨论】:

以上是关于iPhone:JSON 消息未正确创建的主要内容,如果未能解决你的问题,请参考以下文章

SpriteKit SKScene 未正确调整大小以适合 iPhone 12

从 appery 发送时,iPhone 上未收到推送通知消息

Cordova Splashscreen iPhone 6 和 iPhone 6 Plus 未显示

tableview 图像未正确显示 - iOS 8 iPhone App Objective C

.xib 未正确加载(iPhone)

在 iPhone 应用中添加和读取一个 json 文件