【问题标题】:Creating A JSON Object from NSDictionary iPhone从 NSDictionary iPhone 创建一个 JSON 对象
【发布时间】:2013-10-21 14:28:36
【问题描述】:

我想创建一个如下的 JSON 字符串

{
    "veneue": {
        "clientId": "b",
        "name": "d",
        "tagline": "f",
        "phone": "b",
        "address": "d",
        "city": "f",
        "state": "b",
        "zip": "d",
        "twitter": "f",
        "license": "d",
        "imagePath": "f",
        "pickupLocation": "b"
    },
    "drinks": [
        {
            "type": "d",
            "name": "f",
            "servingTable": {
                "servingSize": "b",
                "price": "d"
            }
        },
        {
            "type": "d",
            "name": "f",
            "servingTable": {
                "servingSize": "b",
                "price": "d"
            }
        }
    ]
}

这是我的模型

JSON 字符串应包含两个 JSONObject,venue, drinks

Venue 有下面列出的子对象,如venueDictionary。 其中,Drinks 是一个模型数组 DrinkClass

NSDictionary *venueDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                               @"name"   ,@"xx"//[self.nameVenue text],
                               @"tagLine",@"xx"//[self.taglineVenue text],
                               @"phone"  ,@"xx"//[self.phoneVenue text],
                               @"addres" ,@"xx"//[self.addressVenue text],
                               @"city"   ,@"xx"//[self.cityVenue text],
                               @"state"  ,@"xx"//[self.stateVenue text],
                               @"zip"    ,@"xx"//[self.zipVenue text],
                               @"twitter",@"xx"//[self.twitterVenue text],
                               @"license",@"xx"//[self.licenseVenue text],
                               @"pickUpLocation",@"xx"//[self.pickUpVenue text],
                               @"imagePath",randomImageName,
                               nil];

arrayDrinks = [[NSMutableArray alloc] init]; //SUPPOSE THIS IS DRINK ARRAY



NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                      @"venue", venueDictionary,
                      @"drinks",
                      nil];

1- 如何将其添加到@"drinks" 以创建 JSON 如上所述,其次我使用的是 SBJSONWriter ,如何使用 JSONDictionary 创建字符串。

//SBJsonWriter *writer = [[SBJsonWriter alloc] init];

【问题讨论】:

    标签: objective-c json sbjson


    【解决方案1】:

    您对dictionaryWithObjectsAndKeys 的每次调用都以错误的方式提供参数(您将键设置为值,反之亦然)。

    只需添加arrayDrinks 作为@"drinks" 键的值。

    只需将jsonDictionary 作为要序列化的对象传递(到stringWithObject:)。

    【讨论】:

      【解决方案2】:

      正如@Wain 所说,您以错误的方式传递值。并从你的 jsonDictonary 创建 JSON 使用这个

      #import "SBJsonWriter.h"
      
      ...
      
      SBJsonWriter *jsonWriter = [[SBJsonWriter alloc] init];
      
      NSString *jsonString = [jsonWriter stringWithObject:jsonDictionary];  
      
      [jsonWriter release];
      

      【讨论】:

        【解决方案3】:
        SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
        NSError *error = nil;
        NSArray *jsonObjects = [jsonParser objectWithString:jsonString error:&error];
        
        NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"id" ascending:YES];
        NSArray *sorted = [jsonObjects sortedArrayUsingDescriptors:@[sort]];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-12-18
          • 2019-06-18
          • 1970-01-01
          • 2021-12-24
          • 2018-12-21
          • 1970-01-01
          • 1970-01-01
          • 2013-06-04
          相关资源
          最近更新 更多