三种常用JSON解析性能比较

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/kunkun39/article/details/84619144
今天用了三种三方包来解析JSON文件并试验了其性能:

JSON文件(测试用):
private static String json = "{\"id\":1,\"title\":\"德国黑森林罕见暴雨把路面冲走\",\n" +
" \"questions\":[\n" +
" {\"sequence\":1,\"title\":\"你是那个?\",\"answers\":[{\"result\":\"A:姓名\"},{\"result\":\"B:六哈\"},{\"result\":\"C:积分\"},{\"result\":\"D:金佛额\"}]},\n" +
" {\"sequence\":2,\"title\":\"德国黑森林罕见暴雨把路面冲走\",\"answers\":[{\"result\":\"A:姓名\"},{\"result\":\"B:六哈\"},{\"result\":\"C:积分\"},{\"result\":\"D:金佛额\"}]},\n" +
" {\"sequence\":3,\"title\":\"德国黑森林罕见暴雨把路面冲走\",\"answers\":[{\"result\":\"A:姓名\"},{\"result\":\"B:六哈\"},{\"result\":\"C:积分\"},{\"result\":\"D:金佛额\"}]},\n" +
" {\"sequence\":4,\"title\":\"你是那个\",\"answers\":[{\"result\":\"A:姓名\"},{\"result\":\"B:六哈\"},{\"result\":\"C:积分\"},{\"result\":\"D:金佛额\"}]},\n" +
" {\"sequence\":5,\"title\":\"美海豚伤痕累累疑曾遭虎鲸整只吞食\",\"answers\":[{\"result\":\"A:姓名\"},{\"result\":\"B:六哈\"},{\"result\":\"C:积分\"},{\"result\":\"D:金佛额\"}]},\n" +
" {\"sequence\":6,\"title\":\"美海豚伤痕累累疑曾遭虎鲸整只吞食\",\"answers\":[{\"result\":\"A:姓名\"},{\"result\":\"B:六哈\"},{\"result\":\"C:积分\"},{\"result\":\"D:金佛额\"}]},\n" +
" {\"sequence\":7,\"title\":\"美海豚伤痕累累疑曾遭虎鲸整只吞食\",\"answers\":[{\"result\":\"A:姓名\"},{\"result\":\"B:六哈\"},{\"result\":\"C:积分\"},{\"result\":\"D:金佛额\"}]},\n" +
" {\"sequence\":8,\"title\":\"美海豚伤痕累累疑曾遭虎鲸整只吞食\",\"answers\":[{\"result\":\"A:姓名\"},{\"result\":\"B:六哈\"},{\"result\":\"C:积分\"},{\"result\":\"D:金佛额\"}]},\n" +
" {\"sequence\":9,\"title\":\"美海豚伤痕累累疑曾遭虎鲸整只吞食\",\"answers\":[{\"result\":\"A:姓名\"},{\"result\":\"B:六哈\"},{\"result\":\"C:积分\"},{\"result\":\"D:金佛额\"}]},\n" +
" {\"sequence\":10,\"title\":\"美海豚伤痕累累疑曾遭虎鲸整只吞食\",\"answers\":[{\"result\":\"A:姓名\"},{\"result\":\"B:六哈\"},{\"result\":\"C:积分\"},{\"result\":\"D:金佛额\"}]},\n" +
" {\"sequence\":11,\"title\":\"美海豚伤痕累累疑曾遭虎鲸整只吞食\",\"answers\":[{\"result\":\"A:姓名\"},{\"result\":\"B:六哈\"},{\"result\":\"C:积分\"},{\"result\":\"D:金佛额\"}]},\n" +
" {\"sequence\":12,\"title\":\"美海豚伤痕累累疑曾遭虎鲸整只吞食\",\"answers\":[{\"result\":\"A:姓名\"},{\"result\":\"B:六哈\"},{\"result\":\"C:积分\"},{\"result\":\"D:金佛额\"}]},\n" +
" {\"sequence\":13,\"title\":\"美海豚伤痕累累疑曾遭虎鲸整只吞食\",\"answers\":[{\"result\":\"A:姓名\"},{\"result\":\"B:六哈\"},{\"result\":\"C:积分\"},{\"result\":\"D:金佛额\"}]},\n" +
" {\"sequence\":14,\"title\":\"美海豚伤痕累累疑曾遭虎鲸整只吞食\",\"answers\":[{\"result\":\"A:姓名\"},{\"result\":\"B:六哈\"},{\"result\":\"C:积分\"},{\"result\":\"D:金佛额\"}]},\n" +
" {\"sequence\":15,\"title\":\"美海豚伤痕累累疑曾遭虎鲸整只吞食\",\"answers\":[{\"result\":\"A:姓名\"},{\"result\":\"B:六哈\"},{\"result\":\"C:积分\"},{\"result\":\"D:金佛额\"}]},\n" +
" {\"sequence\":16,\"title\":\"美海豚伤痕累累疑曾遭虎鲸整只吞食\",\"answers\":[{\"result\":\"A:姓名\"},{\"result\":\"B:六哈\"},{\"result\":\"C:积分\"},{\"result\":\"D:金佛额\"}]},\n" +
" {\"sequence\":17,\"title\":\"美海豚伤痕累累疑曾遭虎鲸整只吞食\",\"answers\":[{\"result\":\"A:姓名\"},{\"result\":\"B:六哈\"},{\"result\":\"C:积分\"},{\"result\":\"D:金佛额\"}]},\n" +
" {\"sequence\":18,\"title\":\"美海豚伤痕累累疑曾遭虎鲸整只吞食\",\"answers\":[{\"result\":\"A:姓名\"},{\"result\":\"B:六哈\"},{\"result\":\"C:积分\"},{\"result\":\"D:金佛额\"}]},\n" +
" {\"sequence\":19,\"title\":\"美海豚伤痕累累疑曾遭虎鲸整只吞食\",\"answers\":[{\"result\":\"A:姓名\"},{\"result\":\"B:六哈\"},{\"result\":\"C:积分\"},{\"result\":\"D:金佛额\"}]},\n" +
" {\"sequence\":20,\"title\":\"美海豚伤痕累累疑曾遭虎鲸整只吞食\",\"answers\":[{\"result\":\"A:姓名\"},{\"result\":\"B:六哈\"},{\"result\":\"C:积分\"},{\"result\":\"D:金佛额\"}]}\n" +
" ]\n" +
"}" ;

1 - json-20070829.jar(不知道从网上那里来的),执行时间11s-12s
public void testParseJsonWith_org_json_jar() throws Exception {
long start = System.currentTimeMillis();

for (int loop = 0; loop < 10000; loop++) {

ExaminationDTO examinationDTO = new ExaminationDTO();
JSONObject jExamination = new JSONObject(json);

//handle examination info
int id = jExamination.optInt("id");
String title = jExamination.optString("title");
examinationDTO.setId(id);
examinationDTO.setTitle(title);

//hanle question
JSONArray jQuestions = jExamination.getJSONArray("questions");
for (int i = 0; i < jQuestions.length(); i++) {
JSONObject jQuestion = jQuestions.getJSONObject(i);
int sequence = jQuestion.optInt("sequence");
String questionTitle = jQuestion.optString("title");
QuestionDTO questionDTO = new QuestionDTO();
questionDTO.setSequence(sequence);
questionDTO.setTitle(questionTitle);

//handle answer
JSONArray jAnswers = jQuestion.getJSONArray("answers");
for (int j = 0; j < jAnswers.length(); j++) {
JSONObject jAnswer = jAnswers.getJSONObject(j);
String result = jAnswer.optString("result");
String[] tokens = StringUtils.delimitedListToStringArray(result, ":");
AnswerDTO answerDTO = new AnswerDTO();
answerDTO.setSequence(tokens[0]);
answerDTO.setResult(tokens[1]);
questionDTO.addAnswer(answerDTO);
}

examinationDTO.addQuestion(questionDTO);
}
}

long end = System.currentTimeMillis();
long during = end - start;
System.out.println("10000 times taken time for org.json is " + during);
}

2 - alibaba fastjson-1.1.34.android.jar,执行时间8s-9s
public void testParseJsonWith_fast_json_jar() throws Exception {
long start = System.currentTimeMillis();

for (int loop = 0; loop < 10000; loop++) {

ExaminationDTO examinationDTO = new ExaminationDTO();
JSONObject jExamination = JSON.parseObject(json);

//handle examination info
int id = jExamination.getInteger("id");
String title = jExamination.getString("title");
examinationDTO.setId(id);
examinationDTO.setTitle(title);

//hanle question
JSONArray jQuestions = jExamination.getJSONArray("questions");
for (int i = 0; i < jQuestions.size(); i++) {
JSONObject jQuestion = jQuestions.getJSONObject(i);
int sequence = jQuestion.getInteger("sequence");
String questionTitle = jQuestion.getString("title");
QuestionDTO questionDTO = new QuestionDTO();
questionDTO.setSequence(sequence);
questionDTO.setTitle(questionTitle);

//handle answer
JSONArray jAnswers = jQuestion.getJSONArray("answers");
for (int j = 0; j < jAnswers.size(); j++) {
JSONObject jAnswer = jAnswers.getJSONObject(j);
String result = jAnswer.getString("result");
String[] tokens = StringUtils.delimitedListToStringArray(result, ":");
AnswerDTO answerDTO = new AnswerDTO();
answerDTO.setSequence(tokens[0]);
answerDTO.setResult(tokens[1]);
questionDTO.addAnswer(answerDTO);
}

examinationDTO.addQuestion(questionDTO);
}
}

long end = System.currentTimeMillis();
long during = end - start;
System.out.println("10000 times taken time for alibaba fast json is " + during);
}

3 - jackson-all-1.9.11.jar, 执行时间15s-17s
public void testParseJsonWith_jackson_jar() throws Exception {
long start = System.currentTimeMillis();

for (int loop = 0; loop < 10000; loop++) {
ObjectMapper objectMapper = new ObjectMapper();

ExaminationDTO examinationDTO = new ExaminationDTO();
JsonNode jExamination = objectMapper.readTree(json);

//handle examination info
int id = jExamination.get("id").getIntValue();
String title = jExamination.get("title").getTextValue();
examinationDTO.setId(id);
examinationDTO.setTitle(title);

//hanle question
ArrayNode jQuestions = (ArrayNode) jExamination.get("questions");
for (int i = 0; i < jQuestions.size(); i++) {
JsonNode jQuestion = jQuestions.get(i);
int sequence = jQuestion.get("sequence").getIntValue();
String questionTitle = jQuestion.get("title").getTextValue();
QuestionDTO questionDTO = new QuestionDTO();
questionDTO.setSequence(sequence);
questionDTO.setTitle(questionTitle);

//handle answer
ArrayNode jAnswers = (ArrayNode) jQuestion.get("answers");
for (int j = 0; j < jAnswers.size(); j++) {
JsonNode jAnswer = jAnswers.get(j);
String result = jAnswer.get("result").getTextValue();
String[] tokens = StringUtils.delimitedListToStringArray(result, ":");
AnswerDTO answerDTO = new AnswerDTO();
answerDTO.setSequence(tokens[0]);
answerDTO.setResult(tokens[1]);
questionDTO.addAnswer(answerDTO);
}

examinationDTO.addQuestion(questionDTO);
}
}

long end = System.currentTimeMillis();
long during = end - start;
System.out.println("10000 times taken time for jackson is " + during);

}
展开阅读全文

iOS三种Json方法解析国家气象局API

08-08

博客http://blog.csdn.net/duxinfeng2010/article/details/7842210rnrnTouchJson库解析rn- (IBAction)buttonPressedone:(id)sender rn// 获取API接口rn NSURL *url = [NSURL URLWithString:@"http://m.weather.com.cn/data/101010100.html"];rn// 定义一个NSError对象,用于捕获错误信息rn NSError *error;rn// rn NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error]; rn// NSLog(@"jsonstring--->%@",jsonString);rn// 将解析得到的内容存放字典中,编码格式UTF8,防止取值时候发生乱码rn NSDictionary *rootDic = [[CJSONDeserializer deserializer] deserialize:[jsonString dataUsingEncoding:NSUTF8StringEncoding] error:&error];rn// 因为返回的Json文件有两层,去第二层类容放到字典中去0rn NSDictionary *weatherInfo = [rootDic objectForKey:@"weatherinfo"];rn// 取值打印rn NSLog(@"今天是 %@ %@ %@ 的天气状况是:%@ %@",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"],[weatherInfo objectForKey:@"weather1"],[weatherInfo objectForKey:@"temp1"]);rnrnrnrnSBJson库解析rnrn- (IBAction)buttonPressedtwo:(id)sender rn NSURL *url = [NSURL URLWithString:@"http://m.weather.com.cn/data/101180701.html"];rn NSError *error=nil;rn NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error]; rn SBJsonParser *parser = [[SBJsonParser alloc]init];rn rn NSDictionary *rootDic = [parser objectWithString:jsonString error:&error];rn NSDictionary *weatherInfo = [rootDic objectForKey:@"weatherinfo"];rn rn NSLog(@"今天是 %@ %@ %@ 的天气状况是:%@ %@",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"],[weatherInfo objectForKey:@"weather1"],[weatherInfo objectForKey:@"temp1"]);rn rnrnrniOS5自带解析类解析rn- (IBAction)buttonPressedthree:(id)sender rn NSError *error;rn// 加载一个NSURL对象rn NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.weather.com.cn/data/101180601.html"]];rn// 将请求的url数据放到NSData对象中rn NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];rn// iOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中rn NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];rn// weatherDic字典中存放的数据也是字典型,从它里面通过键值取值rn NSDictionary *weatherInfo = [weatherDic objectForKey:@"weatherinfo"];rn rn NSLog(@"今天是 %@ %@ %@ 的天气状况是:%@ %@",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"],[weatherInfo objectForKey:@"weather1"],[weatherInfo objectForKey:@"temp1"]);rn// 打印出weatherInfo字典所存储数据rn NSLog(@"weatherInfo字典里面的内容是--->%@",[weatherInfo description]);rnrn 论坛

没有更多推荐了,返回首页