Shraddha Harne
Shraddha Harne

Reputation: 95

Iphone:how to create json in Objective c

I am working on iPhone App.I have to return json string to a webservice in following format from Iphone.I am using Objective-C

{
"InspectionDetails":
    [
        {"isCompleted":"Y","QMSStepId":"1A","QMSEmpId":"6","QMSInspectionID":"1","InspectedDate":"07/28/11 09:52:34",       "isNewRoom":"1","RoomInspID":"1","QMSRoomId":"1","QMSScoreId":"4"},
        {"isCompleted":"Y","QMSStepId":"1B","QMSEmpId":"4","QMSInspectionID":"1","InspectedDate":"07/28/11 09:52:34",       "isNewRoom":"1","RoomInspID":"1","QMSRoomId":"1","QMSScoreId":"3"}
    ],
"InspectionComments":
    [
        {"QMSPredefinedCommentId":"1","customText":"Test1 Comment","RoomInspID":"1"},
        {"QMSPredefinedCommentId":"2","customText":"Test2 Comment","RoomInspID":"1"}
    ],
"Tools":
    [
        {"Facility_Code" : "1","HddId" : "AIPH01"}
    ]
}

can any one please help me how can I form the above response?

I have an idea that I can do this by using NSArray and NSDictonary but I want all the arrays in one dictionary. Can anyone please guide?

Thanks, Shradha

Upvotes: 3

Views: 2333

Answers (2)

tilo
tilo

Reputation: 14169

I recommend using JSONKit (https://github.com/johnezang/JSONKit), it works very well and is speedy enough to serve common needs.

Suppose you have a NSDictionary like:

NSDictionary *dict = [NSDictionary dictionaryWithObjects:
                        [NSArray arrayWithObjects:,@"anotherDict",@"anotherDict1",@"anotherDict" nil] 
forKeys:[NSArray arrayWithObjects:@"Key1",@"Key2",@"Key3", nil]];

then you can simply get your JSON representation as follows: NSString *jsonString = [dict JSONString].

Read the documentation to get additional features.

Upvotes: 1

cduhn
cduhn

Reputation: 17918

The easiest way is to use something like SBJSON. Then you can just do

NSString *jsonString = [myDictionary JSONRepresentation];

Upvotes: 1

Related Questions