简体   繁体   English

我如何将request.responseData存储到NSdictionary中进行解析

[英]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. 当我向我的Web服务器请求时,它将在request.response.i中向我发送Json字符串。我想将该json存储到NSDictionary中以进行解析并将其存储到数据库中。 My Json format is 我的Json格式是

{ "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 ?? 如果我使用此代码将字符串按字符串打印到NSlog,则显示正常,但如何将其存储到NDdictionary中?

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

To store into dictionary i tried this code but this store my json in reverse order 为了将其存储到字典中,我尝试了此代码,但是这以相反的顺序存储了我的json

 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. 感谢您的帮助。我正在使用ASIFormDataRequest进行网络连接。

Your JSON is not valid. 您的JSON无效。

I check at 我检查

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

Your array format is wrong. 您的数组格式错误。 Read JSON syntax HERE 这里阅读JSON语法

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 . 然后,要存储JSON数据,建议您使用我的技术HERE Proper way and quite a beast 正确的方法和野兽

  • Json is parsed and output is NSArray or NSDicitonary 解析Json,输出为NSArray或NSDicitonary
  • You added wrong json.Not proper format 您添加了错误的json。格式不正确
  • 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 NSdictionary没有您所说的顺序,它是一个键值对机制,即它使用setObject:ForKey:方法存储为键的值,并在被询问时使用与设置键相同的键将其返回使用objectForKey:方法的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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