Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iphone:how to create json in Objective c

Tags:

json

iphone

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

like image 688
Shraddha Harne Avatar asked Feb 13 '26 21:02

Shraddha Harne


2 Answers

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

NSString *jsonString = [myDictionary JSONRepresentation];
like image 141
cduhn Avatar answered Feb 15 '26 11:02

cduhn


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.

like image 25
tilo Avatar answered Feb 15 '26 10:02

tilo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!