Pergunta

I want to create a JSON String as below

{
    "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"
            }
        }
    ]
}

Here is what my model is

The JSON String should contain two JSONObjects, venue, drinks.

Venue has sub objects listed below as venueDictionary. Where as Drinks is an array of a Model 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- How can I add it to @"drinks" to create a JSON As above, Secondly I am using SBJSONWriter , how to create a string with JSONDictionary.

//SBJsonWriter *writer = [[SBJsonWriter alloc] init];
Foi útil?

Solução

Each call you make to dictionaryWithObjectsAndKeys, you are supplying the parameters the wrong way round (you set the keys as the values and vice-versa).

Just add the arrayDrinks as the value for the @"drinks" key.

Just pass the jsonDictionary as the object to be serialised (to stringWithObject:).

Outras dicas

As @Wain said you are passing values in the wrong way. And to create JSON from your jsonDictonary use this

#import "SBJsonWriter.h"

...

SBJsonWriter *jsonWriter = [[SBJsonWriter alloc] init];

NSString *jsonString = [jsonWriter stringWithObject:jsonDictionary];  

[jsonWriter release];
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]];
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top