0

Iam trying to send one json in POST request.

I created one NSMutableDictionary and added Key Value pairs, i have one NSMutableArray which contains Binary Stream of UIImage.

so When i Add this NSMutableArray into NSMutableDictionary and i did one NSLog

So result is

{
key:value,
key:value1,
ArrayKey:(
iuhdgbdsnb325435435j435bu4h5h45hu34h545ih43h5i43hi5hu3i4h5u3
)

}

but my expected JSON should be look like this

{
    key:value,
    key:value1,
    ArrayKey:[   
    iuhdgbdsnb325435435j435bu4h5h45hu34h545ih43h5i43hi5hu3i4h5u3
    ]

    }

"[" "]" square bracket is correct format, but iam getting "(" ")" .

I need NSMutableArray this in "[" "]" then only my server detect this json(after serialising this dictionary).

Please help my code is;

    UIImage *yourImage= [UIImage imageNamed:@"test.jpg"];
    NSData *imageData = UIImagePNGRepresentation(yourImage);

    dict=[[NSMutableDictionary alloc]init];
    arr1=[[NSMutableArray alloc]init];
    [arr1 addObject:imageData];

    [dict setObject:@"value" forKey:@"key"];
    [dict setObject:@"value1" forKey:@"key1"];
    [dict setObject:arr1 forKey:@"ArrayKey"];

Please help me

abhi bangalore
  • 101
  • 2
  • 10
  • 4
    You are confusing the output of the array's description method with a proper JSON serialization's output. Do not worry about. Just build your JSON properly. And do not abuse the desription for that. – Hermann Klecker May 27 '15 at 07:36
  • 2
    Read it from my lips: Neither NSDictionary nor NSArray display themselfs in the Log in JSON format. That are two different things which just happen to look similar. – Hermann Klecker May 27 '15 at 07:45
  • When you add NSMutableArray to NSMutableDictionary how can you expect it become a JSON object ? If you want a JSON you need to use `NSJSONSerialization`. Check this [answer](http://stackoverflow.com/a/9020923/366346) for more details. – GoodSp33d May 27 '15 at 07:46
  • @HermannKlecker dude my problem is very simple "why am getting ( ) normal brackets when i print array instead of [ ] square bracket" – abhi bangalore May 27 '15 at 07:52
  • @abhibangalore Lol. Why are you expecting JSON format for NSArray ? Both are different. – GoodSp33d May 27 '15 at 07:54
  • Because an NSArray represents itself in regular brackets (). Just because. Your problem is even simplier than that. You do not listen to what a number of people is trying to explain. – Hermann Klecker May 27 '15 at 07:55
  • dude i updated my question (there was a mistake i didn't add serialisation ).. i know for getting NSMutableDictionary in JSON format i need to serialise that .i have updated long before in question that . but still nobody answer to my issue with normal bracket. Array in JSON should represent as [ ] not as () – abhi bangalore May 27 '15 at 07:57
  • Keep calm and follow Fonix's suggestion. – GoodSp33d May 27 '15 at 07:59
  • Include your NSLog statements in the question – Hermann Klecker May 27 '15 at 08:05
  • @abhibangalore You have not serialised your dictionary, which is the reason why you keep getting `()` instead of `[]`. Check my answer. – GoodSp33d May 27 '15 at 08:12
  • @abhibangalore: The NSLog is perfectly fine. There is nothing wrong with it. What's wrong is your expectation what NSLog should print. And calling someone "dude" in this context will usually be taken as attempting an insult. – gnasher729 May 27 '15 at 08:31

5 Answers5

2

printing a dictionary does not output it in json, its only json-like by coincidence, you would need to use the NSJSONSerialization utility to convert it to json, here is how it would be used

Community
  • 1
  • 1
Fonix
  • 11,447
  • 3
  • 45
  • 74
  • iam using nsjson serialisation too..but my problem is that nsmutable array bracket – abhi bangalore May 27 '15 at 07:37
  • are you printing the json string, or the dictionary? post your code how you are NSLogging the json – Fonix May 27 '15 at 07:38
  • even i print dictionary or json the NSMutableArray should be like [ ] in square bracket not in normal ( ) brackets – abhi bangalore May 27 '15 at 07:41
  • right now am getting NSMutableArray in ( ) and this is wrong – abhi bangalore May 27 '15 at 07:41
  • if you are printing the string like in the link i pasted in my answer, the brackets should be correct. please show how you are printing it – Fonix May 27 '15 at 07:44
  • After adding that jsonString Lines it crashed with this message Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (NSConcreteMutableData)' – abhi bangalore May 27 '15 at 07:48
  • 1
    @abhibangalore You are adding NSData to JSON object. Not sure if you can add data to JSON – GoodSp33d May 27 '15 at 07:50
  • @abhibangalore, i think you are adding a type of object to the dictionary that the `JSONSerializer` doesnt know how to serialize, you can only add basic types to the nsdictionary if you want it to be converted to json, so your UIImage cant be converted to json, you will have to base64 encode your image into a string, and put that into your dictionary instead, then decode it on the server side – Fonix May 27 '15 at 07:52
  • [read this](https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSJSONSerialization_Class/) – Fonix May 27 '15 at 07:55
  • so i can remove that < > from NSData and add it into NSMutableArray right? – abhi bangalore May 27 '15 at 07:55
0

When you print the content of an object to NSLog or in the debugger, then in the background the description method is called.

Give it a try:

NSString aString = [dict description]; 
NSLog ("My string: %@", aString);
NSLog ("My dict:   %@", dict); 

Both will look the same. The output just happens to remind you a JSON structure but it is not. Use a JSON serialisation framework for that. NSJSONSerialization is around for a while now and can savely be used.

Hermann Klecker
  • 14,039
  • 5
  • 48
  • 71
0

Try This

    NSError *error;
    NSData * jsonData = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:&error];
    NSString *dataString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

Now your dataString will be a string in JSON format

Shruti
  • 1,849
  • 1
  • 13
  • 21
  • Hello boss my problem is why am getting normal brackets instead if square bracket . – abhi bangalore May 27 '15 at 07:55
  • @abhibangalore Because that's how the `[NSArray description]` method decided to write it. How many times do you need to be told this? It doesn't have to conform to JSON as JSON is just one way to represent data. – Droppy May 27 '15 at 08:21
  • @abhibangalore.. you have to `serialise` your `dictionary` like this then only `json` is created and you will get [] instead of () . Try to serialise it once. – Shruti May 27 '15 at 08:44
0

You have not serialised your NSDictionary which is why you keep seeing () instead of [].

NSMutableDictionary *jsonDictionary = [NSMutableDictionary new];
NSMutableArray *array = [NSMutableArray new];

[array addObject:@"arrayData1"];
[array addObject:@"arrayData2"];
[array addObject:@"arrayData3"];

[jsonDictionary setObject:@"abhi" forKey:@"name"];
[jsonDictionary setObject:@"bangalore" forKey:@"city"];
[jsonDictionary setObject:array forKey:@"arrayKey"];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary
                                                   options:NSJSONWritingPrettyPrinted
                                                     error:nil];
NSLog(@"Dictionary in JSON Format: %@", [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);
NSLog(@"Dictionary Output: %@", jsonDictionary);

From Console:

2015-05-27 13:40:15.344 For Abhi[2086:51652] Dictionary in JSON Format: {
  "name" : "abhi",
  "city" : "bangalore",
  "arrayKey" : [
    "arrayData1",
    "arrayData2",
    "arrayData3"
  ]
}
2015-05-27 13:40:15.345 For Abhi[2086:51652] Dictionary Output: {
    arrayKey =     (
        arrayData1,
        arrayData2,
        arrayData3
    );
    city = bangalore;
    name = abhi;
}

Notice that the Dictionary output has () where was JSON data (serialised from Dictionary) has [].

GoodSp33d
  • 6,252
  • 4
  • 35
  • 67
0

You say your server expects output like this:

{
    key:value,
    key:value1,
    ArrayKey:[   
        iuhdgbdsnb325435435j435bu4h5h45hu34h545ih43h5i43hi5hu3i4h5u3
    ]
}

That's not JSON. There is nothing in iOS to produce output like that, and there is no way a server should expect that kind of output.

And objects that get serialized to JSON must only contain: NSDictionary, NSArray, NSString, NSNumber and NSNull. No NSData. You should first ask your server people what they are actually expecting.

gnasher729
  • 51,477
  • 5
  • 75
  • 98