Iphone:如何在Objective c中创建json

时间:2011-08-09 06:45:36

标签: iphone json

我正在使用iPhone App。我必须将以下格式的json字符串从Iphone返回到web服务。我正在使用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"}
    ]
}

任何人都可以帮助我如何形成上述回复?

我有一个想法,我可以通过使用NSArray和NSDictonary来做到这一点,但我想在一个字典中的所有数组。有人可以指导吗?

谢谢, Shradha

2 个答案:

答案 0 :(得分:1)

最简单的方法是使用SBJSON之类的东西。那你就可以做到

NSString *jsonString = [myDictionary JSONRepresentation];

答案 1 :(得分:1)

我建议使用JSONKit(https://github.com/johnezang/JSONKit),它运行良好,速度足以满足常见需求。

假设您有一个NSDictionary,如:

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

然后您可以按如下方式获取JSON表示:NSString *jsonString = [dict JSONString]

阅读文档以获取其他功能。