0

When i request to my web server it send me Json string in request.response.i want to store that json into NSDictionary for parsing and storing it into database. My Json format is

{ "rowNumber" : 3,
   [ { "Age" : "2 - 4 years old ",
   "AndroidID" : "2",
   "Category" : "Chanson",
   "Description" : "fourni",
   "Size" : 3447196,
   "Thumbnail" : null,
   "Title" : "test",
   "iTunesID" : "2",
   "inactive" : false,
   "product_id" : 2} ],

   [ { "Age" : "2 - 4 years old ",
   "AndroidID" : "3",
   "Category" : "Chanson",
   "Description" : "Animation ",
   "Size" : 3447196,
   "Thumbnail" : null,
   "Title" : "Escargot",
   "iTunesID" : "3",
   "inactive" : false,
   "product_id" : 3
    } ] 
     }

IF i use this code to print String by string to NSlog it display fine but How i can i store that into NDdictionary ??

NSString *response = [[request responseString] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

To store into dictionary i tried this code but this store my json in reverse order

 NSDictionary* json = [NSJSONSerialization
                      JSONObjectWithData:request.responseData

                      options:kNilOptions
                      error:&error];

for(NSString *key in [json allKeys]) {
    NSLog(@"%@",[json objectForKey:key]);

it store it into reverse order. Any help is appreciated.I am using ASIFormDataRequest for networking.

Swap-IOS-Android
  • 4,363
  • 6
  • 49
  • 77
  • 1
    `NSDictionary` is not an ordered collection. If you wish to order it, sort the keys using a `NSArray`. – alex-i Jun 05 '13 at 09:39
  • 2
    Your json is not in valid format. – Dilip Manek Jun 05 '13 at 09:48
  • @alex-i Can you please explain me in details?..do you have example.. – Swap-IOS-Android Jun 05 '13 at 09:51
  • Ideally an array should be comprised of objects of same type. Your json is not in a valid format, it's very unclear for what purpose the "rowNumber" is there. Your product is inside an array for no apparent reason. Given these details we can give some suggestions. – Anupdas Jun 05 '13 at 09:58

2 Answers2

1

Your JSON is not valid.

I check at

  1. http://jsonviewer.stack.hu/
  2. http://jsonviewer.net/

Your array format is wrong. Read JSON syntax HERE

This is how it should be done:

{ "rowNumber" : 3,
   "Data" : [ { 
    "Age" : "2 - 4 years old ",
   "AndroidID" : "2",
   "Category" : "Chanson",
   "Description" : "fourni",
   "Size" : 3447196,
   "Thumbnail" : null,
   "Title" : "test",
   "iTunesID" : "2",
   "inactive" : false,
   "product_id" : 2 
   } ,
   { 
   "Age" : "2 - 4 years old ",
   "AndroidID" : "3",
   "Category" : "Chanson",
   "Description" : "Animation ",
   "Size" : 3447196,
   "Thumbnail" : null,
   "Title" : "Escargot",
   "iTunesID" : "3",
   "inactive" : false,
   "product_id" : 3
    } ] 
}

Then to store JSON data, I recommend you using my technique HERE. Proper way and quite a beast

Community
  • 1
  • 1
Alphapico
  • 2,893
  • 2
  • 30
  • 29
  • @Malcolm yes the json format which you give me is valid one.. when i request to my server server response me this json with N number of Data item..rownumber is used for how many item i want to display in row and DATA is array of item that i want to display in tableview... – Swap-IOS-Android Jun 05 '13 at 10:41
  • When i request to my server to give me list for tableview at that request it send me direct json (no url of json) – Swap-IOS-Android Jun 05 '13 at 10:43
0
  • Json is parsed and output is NSArray or NSDicitonary
  • You added wrong json.Not proper format
  • NSdictionary doesn't have an order as you say.It is a key-value pair mechanism.That is it stores as a value for a key using setObject:ForKey: method and gives the value back when asked with same key used to set the value using objectForKey: method
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101