Converting NSString to NSDictionary / JSON

前端 未结 5 1587
时光说笑
时光说笑 2020-12-02 07:05

I have the following data saved as an NSString :

 {
    Key = ID;
    Value =         {
        Content = 268;
        Type = Text;
    };
},
          


        
5条回答
  •  独厮守ぢ
    2020-12-02 07:27

    I believe you are misinterpreting the JSON format for key values. You should store your string as

    NSString *jsonString = @"{\"ID\":{\"Content\":268,\"type\":\"text\"},\"ContractTemplateID\":{\"Content\":65,\"type\":\"text\"}}";
    NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    

    Now if you do following NSLog statement

    NSLog(@"%@",[json objectForKey:@"ID"]);
    

    Result would be another NSDictionary.

    {
        Content = 268;
        type = text;
    }
    

    Hope this helps to get clear understanding.

提交回复
热议问题