json处理之org.codehaus.jackson
·
我在写subscriptionEvent接口时,使用的解析post json内容时,使用的是org.codehaus.jackson来解析的:
接收的post json请求体:
{subscriptionEvents:[{"subscriptionTier":"DELETE","startDate":null,"transactionId":"1111111","paymentType":null,"expiryDate":null,"userId":"111111111111111111111","countryCode":"CN","serviceCode":null},{"subscriptionTier":"DELETE","startDate":null,"transactionId":"222222222222222222","paymentType":null,"expiryDate":null,"userId":"2222222222222222222","countryCode":"CN","serviceCode":null},{"subscriptionTier":"DELETE","startDate":null,"transactionId":"3333333333","paymentType":null,"expiryDate":null,"userId":"333333333333333333333333","countryCode":"CN","serviceCode":null}]}
import java.io.ByteArrayOutputStream;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
//request---json串
public static String getStreamString(HttpServletRequest request) {
final int BUFFER_SIZE = 8 * 1024;
String s = null;
byte[] buffer = new byte[BUFFER_SIZE];
try {
ServletInputStream inputStream = request.getInputStream();
int length = 0;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
do {
length = inputStream.read(buffer);
if (length > 0) {
baos.write(buffer, 0, length);
}
} while (length * 2 == BUFFER_SIZE);
s = new String(baos.toByteArray());
} catch (Exception e) {
s=null;
log.error("while get string values from inputstream,Exception!!!");
e.printStackTrace();
}
return s;
}
处理json的操作方法:
String bodyData = getStreamString(request);//得到json字符串
package com.ultimate.ws.util;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ultimate.ws.pojo.Subscriptionevent;
public static List<Subscriptionevent> getSubscriptionEvent(String jsonData) {
JsonNode root;
List<Subscriptionevent> tempList = null;
/*jsonData ="{\"subscriptionEvent\":"+ "["+ "{"+
"\"userId\":\"sdffgw3rsh\","+ "\"countryCode\":\"AU\","+
"\"subscriptionTier\": \"PREMIUM-12-25 13:29:01\","+
"\"paymentType\": \"VOUCHER\","+
"\"startDate\": \"2015-03-29 13:29:31\","+
"\"expiryDate\": \"2015-04-29 23:11:01\","+
"\"transactionId\": \"20139630776768\""+
"},"+ "{"+ "\"userId\":\"sdffgw3rsh\","+ "\"countryCode\":\"AU\","+
"\"subscriptionTier\": \"PREMIUM-12-25 13:29:01\","+
"\"paymentType\": \"VOUCHER\","+
"\"startDate\": \"2015-03-29 13:29:31\","+
"\"expiryDate\": \"2015-04-29 23:11:01\","+
"\"transactionId\": \"20139630776768\""+ "}]}";*/
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
try {
root = mapper.readTree(jsonData);
JsonNode data = root.path("subscriptionEvents");
Iterator<JsonNode> it = data.iterator();
tempList = new ArrayList<Subscriptionevent>();
Date date = new Date();
while (it.hasNext()) {
// flag = true;
// JsonNode node = it.next();
//这里使用了Gson 类来转换为定义类
Subscriptionevent t = (Subscriptionevent) = gson.fromJson(it.next().toString(), Subscriptionevent.class);
tempList.add(t);
}
} catch (Exception e) {
e.printStackTrace();
}
if (tempList!=null&&tempList.size() > 0) {
return tempList;
}
return null;
}
总结:
在使用过程中,当调用时传递的数组内容达到一定限度后,就会被截断,报错内容是解析格式异常。正常传递参数时,不会往这个json数组中传递多个对象,一般是一个,所以没有问题,但当大批量操作时,一次调用就不行,还要将这个数组拆分成多个,多次调用才行,具体这个限制长度应该是这个json处理类内部做了设置,我没有具体去查或者去设置
更多推荐
所有评论(0)