[英]How to properly Append NSMutableData and NSData with JSONSerialization?
im trying to append nsdata to nsmutabledata, on my first attempt, it seems fine, but when i get new contents of nsdata and append it to my nsmutabledata, it seems fine, but my deserialized dictionary returns nil, please help guys. 我尝试在第一次尝试将nsdata附加到nsmutabledata时,看起来不错,但是当我获得nsdata的新内容并将其附加到我的nsmutabledata时,看起来不错,但是我的反序列化字典返回nil,请帮助大家。 :)
:)
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:myMutableDictionary options:0 error:nil];
NSMutableData * myMutableData = [NSMutableData dataWithData:jsonData];
[myMutableData appendData:jsonData];
NSDictionary *deSerializedDict = [NSJSONSerialization JSONObjectWithData:myMutableData options:NSJSONReadingMutableContainers error:nil];
You should write one JSON function.Why do you want to add NSData to NSMutableData?. 您应该编写一个JSON函数,为什么要将NSData添加到NSMutableData? If you follow below coding,you can understand easily.
如果遵循以下编码,则可以轻松理解。
//just give your URL instead of my URL
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://stackoverflow.com/questions/30662452/how-to-properly-append-nsmutabledata-and-nsdata-with-jsonserialization"]];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];
NSError *err;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];
NSLog("the jsonDict is - %@",jsonDict);
As an absolute, one hundred percent principle, if you have NSData containing a valid JSON object, then after adding anything but white space characters the NSData cannot possibly contain a valid JSON object anymore. 作为一个绝对的100%原则,如果您的NSData包含有效的JSON对象,则在添加除空格字符之外的任何内容之后,NSData都可能不再包含有效的JSON对象。
As a special case, if you have TWO NSData containing valid JSON objects, then their concatentation can NEVER contain a valid JSON object. 作为一种特殊情况,如果您有两个包含有效JSON对象的NSData,则它们的结合绝不能包含有效的JSON对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.