sai sai - 3 months ago 9
JSON Question

Create Custom JSON STRING

I want to send a data to server using JSON string. The following is the format of JSON object I want to use. Please help me to create this type of structure.

{ '__metadata': { 'type': 'SP.Data.MetrolinkVerificationListItem' },
'Company': 'SOCALGAS-15',
'Date': '08/31/2016',
'Employee_x0020_ID': '545',
'Month': 'JULY',
'Name_x0020_of_x0020_Transit': 'METROLINK',
'Total_x0020_Amount_x0020_Spent': '444',
'Year': '2015'}

Answer

if you want to pass the NSDictionary as param on your request use this

NSDictionary *type = @{ @"type" : @"SP.Data.MetrolinkVerification‚ÄĆ‚ÄčListItem"
                       };

NSDictionary *final = @{@"metadata" : type, @"pas" : @"1234",@"name" : @"hai"
                       };

NSLog(@"json string %@",final);

output

enter image description here

if you want to pass the NSDictionary to JSON String as param on your request use this

NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:final
                                                   options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                                                     error:&error];

if (! jsonData) {
    NSLog(@"Got an error: %@", error);
} else {
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

     NSLog(@"final %@",jsonString);
}

output

enter image description here