可以详见博客https://blog.csdn.net/zlsuperjj/article/details/80039143
java中Xml、json之间的相互转换可参见博客:https://www.cnblogs.com/tv151579/p/3516674.html
1.第一种方法(个人实现的)
使用JSON-JAVA提供的方法,之前一直使用json-lib提供的方法转json,后来发现了这个开源项目,觉得用起来很不错,并且可以修改XML.java中的parse方法满足自己的转换需要。
(1)首先去git下载所需的java文件,并导入项目
Git:https://github.com/stleary/JSON-java
(2)使用XML.java中提供的XML.toJSONObject(xml)方法即可完成xml到json的转换,同时也可以对JSON进行格式化
[java] view plain copy /* 第一种方法,使用JSON-JAVA提供的方法 */ //将xml转为json JSONObject xmlJSONObj = XML.toJSONObject(xml); //设置缩进 String jsonPrettyPrintString = xmlJSONObj.toString(4); //输出格式化后的json System.out.println(jsonPrettyPrintString);2.第二种方法
使用json-lib的XMLSerializer对象
(1)创建XMLSerializer对象
(2)使用XMLSerializer的read(xml)方法即可
[java] view plain copy /* 第二种方法,使用json-lib提供的方法 */ //创建 XMLSerializer对象 XMLSerializer xmlSerializer = new XMLSerializer(); //将xml转为json(注:如果是元素的属性,会在json里的key前加一个@标识) String result = xmlSerializer.read(xml).toString(); //输出json内容 System.out.println(result);
3.测试
[java] view plain copy public class Test { public static void main(String[] args) { String xml = "<class id=" + "'1'" + "><student><name>aaaaaa</name><age>21</age></student><student><name>bbbbbb</name><age>22</age></student></class>"; /* 第一种方法,使用JSON-JAVA提供的方法 */ //将xml转为json JSONObject xmlJSONObj = XML.toJSONObject(xml); //设置缩进 String jsonPrettyPrintString = xmlJSONObj.toString(4); //输出格式化后的json System.out.println(jsonPrettyPrintString); /* 第二种方法,使用json-lib提供的方法 */ //创建 XMLSerializer对象 XMLSerializer xmlSerializer = new XMLSerializer(); //将xml转为json(注:如果是元素的属性,会在json里的key前加一个@标识) String result = xmlSerializer.read(xml).toString(); //输出json内容 System.out.println(result); }
第一种方法输出:
[java] view plain copy {"class": { "id": 1, "student": [ { "age": 21, "name": "aaaaaa" }, { "age": 22, "name": "bbbbbb" } ] }}第二种方法输出:
[java] view plain copy {"@id":"1","student":[{"name":"aaaaaa","age":"21"},{"name":"bbbbbb","age":"22"}]}源码下载:http://download.csdn.net/detail/lom9357bye/9690304
json转换成model技巧
在项目中从服务器获取数据的时候,经常采用json格式的数据,为了能方便的使用这些数据,项目中会把json转化为model。 之前我们的项目中是这么用的: 很复杂,很乱,对不对,然而这才只是二十多个字段中的5个,(:зゝ∠),一定有更好的方法。 如果我们能够在动态地去获取对象的属性名就好了,只要json的key和对象的属性名一样,那么就可以利用for循环以可kvc赋值了。在Java中,我们可以利用反射来做到这一点,同样的,在OC中,我们可以利用runtime的特性来实现。 代码如下: NSDictionary *dic = @{@"name" : @"小王", @"no" : @"123", @"rank" : @1, @"sex" : @0}; Student *student = [[Student alloc] init]; u_int count; objc_property_t *properties =class_copyPropertyList([Student class], &count); for (int i = 0;i<count;i++) { const char* propertyName =property_getName(properties[i]); NSString *property = [NSString stringWithUTF8String: propertyName]; [student setValue:dic[property] forKey:property]; } free(properties)结果如下,DONE!:
优点:这种扩展性强,并且与dic存储的类型无关,例如将@"rank" : @1改成@"rank" : @"1"后,也能得到正确的输出。 缺点;但是要求属性名和接口返回json数据中的key一一对应。 扩展一 封装一下,让Model类继承基类就可以直接使用。创建一个JsonModel类,代码如下: #import "JsonModel.h" #import "objc/runtime.h" @implementation JsonModel - (id)initWithDic:(NSDictionary *)dic { self = [super init]; if (self) { u_int count; objc_property_t *properties =class_copyPropertyList([self class], &count); for (int i = 0;i<count;i++) { const char* propertyName =property_getName(properties[i]); NSString *property = [NSString stringWithUTF8String: propertyName]; [self setValue:dic[property] forKey:property]; } free(properties); } return self; } @end
使用时,让Student类继承JsonModel类,然后就可以方便的初始化Student类了。 Student *student = [[Student alloc] initWithDic:dic]; 扩展二 如果由于各种原因,接口返回数据的key和model的属性名不能统一,那么如何处理呢?初步想法是构造一个映射关系,代码如下: #import "JsonModel.h" #import "objc/runtime.h" @implementation JsonModel - (id)initWithDic:(NSDictionary *)dic { self = [super init]; if (self) { u_int count; objc_property_t *properties =class_copyPropertyList([self class], &count); for (int i = 0;i<count;i++) { const char* propertyName =property_getName(properties[i]); NSString *property = [NSString stringWithUTF8String: propertyName]; [self setValue:dic[property] forKey:property]; } free(properties); } return self; } @end
使用方法:通过重写静态方法getKeyAndPropertyCorrelation去设置关系映射表。 扩展三 github上有一个开源Json解析第三方库JsonModel,包含了很多强大的功能。
转载于:https://www.cnblogs.com/EST-woah/p/10621050.html