简体   繁体   中英

How can i store request.responseData into NSdictionary for parsing

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.

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

  • 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM