首页 > 代码库 > ios 中 数组、字典转成json格式上传到后台,遇到的问题
ios 中 数组、字典转成json格式上传到后台,遇到的问题
项目需求需要传json格式到后台
字典转成JSON
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000; min-height: 21.0px } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #6122ae } span.s1 { } span.s2 { color: #3c828b } span.s3 { color: #539aa4 } span.s4 { color: #000000 } span.s5 { color: #3e1e81 } span.s6 { color: #c81b13 } span.s7 { color: #6122ae }</style>AddGoodModel*model = _dataArray[i];
NSMutableDictionary*guigeDict =[NSMutableDictionary dictionary];
[guigeDict setValue:model.name forKey:@"name"];
[guigeDict setValue:model.isck forKey:@"isck"];
[guigeDict setValue:groupPrice forKey:@"groupprice"];
[guigeDict setValue:model.price forKey:@"price"];
[guigeDict setValue:model.storage forKey:@"storage"];
[guigeDict setValue:[NSString stringWithFormat:@"%ld",model.AddGoodID] forKey:@"id"];
[guigeDict setValue:model.commonid forKey:@"commonid"];
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1e9421 } span.s1 { }</style>
NSDictionary*parameter =@{@"":[[NSString alloc]initWithData:[NSJSONSerialization dataWithJSONObject:guigeDict options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding]};
注意事项:字典不能是
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000 } span.s1 { }</style>[guigeDict setObject:@”“ forKey:@”“]; 方式赋值,不然会出现乱码的情况
数组转成JSON
数组转成json只能转成字符串的形式,注意好像不能是可变数组,只能是不可变数组
,不然会出现问题
<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #000000 } span.s1 { } span.s2 { color: #6122ae } span.s3 { color: #3e1e81 } span.s4 { color: #539aa4 } span.s5 { color: #294c50 }</style>NSArray*jsonArray =[NSArray arrayWithArray:_NewArray];
NSString*string =[jsonArray JSONString];
注意:接收转换后的JSON格式数据一定要用相应类型接收赋值,不然会数据类型不对崩溃
ios 中 数组、字典转成json格式上传到后台,遇到的问题