IOS学习系列DATA:一 、JSON数据 (用自带API)
来源:互联网 发布:淘宝店铺动态评分4.6 编辑:程序博客网 时间:2024/06/08 03:47
http://www.cnblogs.com/whzhao2000/archive/2011/12/15/2289010.html
#import <Foundation/NSJSONSerialization.h>
//init the url
NSMutableString *urlstring = [NSMutableString stringWithCapacity:200];
if(txtuserid == nil)
{
[urlstring appendString:@"http://172.21.2.80:8068/json/GetUserinfo.aspx"];
//@"http://172.21.2.80:8068/json/GetUserinfo.aspx"];
}
else
{
[urlstring appendString:@"http://172.21.2.80:8068/json/GetUserinfo.aspx?userid="];
[urlstring appendString:txtuserid.text];
}
NSURL *jsonURL = [NSURL URLWithString:urlstring];//1
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF8);//编码
NSString *jsonData = [[NSString alloc] initWithContentsOfURL:jsonURL encoding:enc error:nil];
if (jsonData == nil) {
NSLog(@"The webservice you are accessing is down.");
}else
{
NSLog(@"json data:%@",jsonData);
//------ios
NSData* data = [NSData dataWithContentsOfURL:jsonURL];//2
NSError* error;
//NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@"dept:%@",json);
NSArray* jsonarr = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];//3
NSLog(@"dept:%@",[[jsonarr objectAtIndex:0] objectForKey:@"dept"]);
NSLog(@"dept:%@",[[jsonarr objectAtIndex:0] objectForKey:@"Name"]);
//------ios
txtdept.text = [[jsonarr objectAtIndex:0] objectForKey:@"dept"];
txtname.text = [[jsonarr objectAtIndex:0] objectForKey:@"Name"];
[txtuserid resignFirstResponder];
参考http://www.raywenderlich.com/5492/working-with-json-in-ios-5
-以下是SOCKET 方式取数时碰到中文编码的问题的解决版本---------------------------------------------------------------------------------------------------
显式采用中文编码 NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);//编码
---------------------------------------------------
NSString *inputMsgStr = self.inputMsg.text;
NSString * content = [inputMsgStr stringByAppendingString:@"\r\n"];
NSLog(@"%a",content);
NSError* error =nil;
// id result = [NSJSONSerialization dataWithJSONObject:self options:kNilOptions error:&error];
//NSData *data2 = @"{"message":"123","userID":"zgc","telType":1}";
//NSData *data = [content dataUsingEncoding:NSISOLatin1StringEncoding];
NSMutableString *msg = [NSMutableString stringWithCapacity:500];
[msg appendString: @"{\'message\':\'"];
[msg appendString: inputMsgStr];
[msg appendString: @"\',"];
[msg appendString: @"\'userID\':\'"];
[msg appendString: @"zwh"];
[msg appendString: @"\',"];
[msg appendString: @"\'telType\':"];
[msg appendString: @"1"];
[msg appendString: @"}\r\n"];
NSData *data = [msg dataUsingEncoding:NSUTF8StringEncoding];
-------------------------------------------------------------------------------------------------
socket 传输JASON数据的编码问题解决方式
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
//NSData * strdata = [data subdataWithRange:NSMakeRange(0, [data length])];
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);//编码
//NSString* aStr = [[NSString alloc] initWithData:strdata encoding:NSASCIIStringEncoding ];
NSString* aStr = [[NSString alloc] initWithData:data encoding:enc ];
data = [aStr dataUsingEncoding:NSUTF8StringEncoding];
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@"dept:%@",json);
//NSArray* jsonarr = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(@"meessage:%@",[json objectForKey:@"message"]);
NSMutableString *msg = [NSMutableString stringWithCapacity:500];
[msg appendString: @"用户名:"];
[msg appendString: [json objectForKey:@"userID"]];
[msg appendString: @"消息:"];
[msg appendString: [json objectForKey:@"message"] ];
//NSLog(@"Hava received datas is :%@",aStr);
self.outputMsg.text = msg;
[client readDataWithTimeout:-1 tag:0];
}
--------补上写json用于保存------------------------------------------------------------------------------------------
- (IBAction)touchWriteButton:(id)sender {
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setValue:@"Anthony" forKey:@"First Name"];
[dictionary setValue:@"Robbins" forKey:@"Last Name"];
[dictionary setValue:[NSNumber numberWithUnsignedInteger:51] forKey:@"Age"];
NSArray *arrayOfAnthonysChildren = [[NSArray alloc] initWithObjects:@"Anthony's Son 1", @"Anthony's Daughter 1", @"Anthony's Son 2", @"Anthony's Son 3", @"Anthony's Daughter 2", nil];
[dictionary setValue:arrayOfAnthonysChildren forKey:@"children"];
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];
if (error) {
NSLog(@"dic->%@",error);
}
[dictionary release];
BOOL succeed = [jsonData writeToFile:JSON_PATH atomically:YES];
if (succeed) {
NSLog(@"Save succeed");
}else {
NSLog(@"Save fail");
}
}
-----------------------读的------------------
- (IBAction)touchReadButton:(id)sender {
NSData *jsonData = [[NSData alloc] initWithContentsOfFile:JSON_PATH]; /* Now try to deserialize the JSON object into a dictionary */
NSError *error = nil;
id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error];
if (jsonObject != nil && error == nil){
NSLog(@"Successfully deserialized...");
if ([jsonObject isKindOfClass:[NSDictionary class]]){
NSDictionary *deserializedDictionary = (NSDictionary *)jsonObject;
NSLog(@"Dersialized JSON Dictionary = %@", deserializedDictionary);
} else if ([jsonObject isKindOfClass:[NSArray class]]){
NSArray *deserializedArray = (NSArray *)jsonObject;
NSLog(@"Dersialized JSON Array = %@", deserializedArray);
} else {
NSLog(@"An error happened while deserializing the JSON data.");
}
}
[jsonData release];
}
- IOS学习系列DATA:一 、JSON数据 (用自带API)
- IOS学习JSON数据(用自带API)
- android自带Json API
- ios自带的json对请求数据进行解析
- iOS 自带扫描api
- Android自带的Json API
- ios进行restful接口调用和json数据利用自带NSJSONSerialization进行数据解析
- NSJSONSerialization iOS自带解析json
- iOS 自带方法解析json
- 使用 IOS 自带 API 扫描二维码
- IOS开发学习27 ObjectC 自带json解析方法的使用
- IOS自带json解析之生成json对象
- iOS 系统自带JSON转字典 字典转JSON
- Java-json系列(一):用GSON解析Json格式数据
- Java-json系列(一):用GSON解析Json格式数据
- Java-json系列(一):用GSON解析Json格式数据
- Java-json系列(一):用GSON解析Json格式数据
- (转)在Android中使用自带API操作Json
- c++运算符重载2
- 不要以为她是个傻女人,她用自己的方式和信念去爱你
- Jquery Table 的基本操作
- js点击换一张验证码
- 上传发布Android应用到各大开放平台市场上去
- IOS学习系列DATA:一 、JSON数据 (用自带API)
- 第十七周项目3胖子伤不起
- tomcat 修改端口后 Eclipse 启动超时
- myeclipse trial expired解决,注册码自动生成
- 字母游戏
- 在jsp页面传值不能用$符号取值,需要用<%= %>的方式来取值.
- LOCAL环境变量
- TNS-01201: Listener cannot find executable /bee/app/oracle/product/11g/bin/oracle for SID
- 为什么需要软件开发报告