json转换Exception
@RequestMapping("/getInitDownstreamCustomerInfo")
public JsonResult getInitDownstreamCustomerInfo(){
JsonResult result = new JsonResult(CommonResultCode.SUCCESS);
String url="http://hwpre-gw.380star.com/api/cloud-goods"+BusinessPC_CustomerInfo_Url;//预发布
// String url="http://gw4test.380star.com/api/cloud-goods/"+BusinessPC_CustomerInfo_Url;测试
// 获取httpclient
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = null;
HttpPost httpPost = new HttpPost(url);
String postResult = null;
try {
LOGGER.info("BusinessPC|getInitDownstreamCustomerInfo|请求:url=" + url );
// 设置请求和传输超时时间
RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(2000).setConnectTimeout(2000).build();
httpPost.setConfig(requestConfig);
//发起请求
response = httpclient.execute(httpPost);
// 得到响应信息
int statusCode = response.getStatusLine().getStatusCode();
// 判断响应信息是否正确
if (statusCode != HttpStatus.SC_OK) {
// 终止并抛出异常
httpPost.abort();
throw new RuntimeException("HttpClient,error status code :" + statusCode);
}
HttpEntity entity = response.getEntity();
if (entity != null) {
postResult = EntityUtils.toString(entity, "UTF-8");
}
EntityUtils.consume(entity);
// String jsonStr = JSONObject.toJSONString(postResult.toString());
String jsonStr = JSON.toJSON(postResult).toString();
List<BussinessPCCustomersInfo> customerInfos= JSONObject.parseArray(jsonStr, BussinessPCCustomersInfo.class);
LOGGER.info("BusinessPC|getInitDownstreamCustomerInfo|:获取下游客户信息数组 customersInfos=" + customerInfos.toString() );
int getAll=0;
if(customerInfos!= null && customerInfos.size()!=0){
getAll= bussinessPCCustomerInfoService.getInitDownstreamCustomerInfo(customerInfos);
result.setMsg("获取现有下游客户信息插入企业云客户信息成功!");
result.setData(getAll);
}else{
result.setMsg("获取现有下游客户信息插入企业云客户信息失败!");
result.setData(getAll);
LOGGER.error("BusinessPC|getInitDownstreamCustomerInfo|获取现有下游客户信息插入企业云客户信息失败", postResult.toString());
throw new Exception("BusinessPC|getInitDownstreamCustomerInfo|获取现有下游客户信息插入企业云客户信息失败"+postResult.toString());
}
LOGGER.info("BusinessPC|getInitDownstreamCustomerInfo|获取现有下游客户信息插入企业云客户信息,resp="+postResult.toString());
}catch (Exception e){
LOGGER.error("BusinessPC|getInitDownstreamCustomerInfo|获取现有下游客户信息插入企业云客户信息", e);
}
return result;
}
使用JSON.toJSON接收com.alibaba.fastjson的JsonArray对象
[
{
“appName”: “中粮人保”,
“enterprisePhone”: “15521293437”,
“id”: 2,
“mobile”: “15521293437”,
“userName”: “blackunique”
},
{
“appName”: “北京脉乐生活餐饮管理有限公司”,
“enterprisePhone”: “13102437172”,
“id”: 772811,
“mobile”: “13102437172”,
“userName”: “脉乐生活”
},
{
“appName”: “企业云zjh0520”,
“enterprisePhone”: “15885218521”,
“id”: 772812,
“mobile”: “18996328523”,
“userName”: “企业云zjh0520”
},
{
“appName”: “福建靠谱邻居网络科技有限公司”,
“enterprisePhone”: “18711124782”,
“id”: 772813,
“mobile”: “17028320952”,
“userName”: “kaopulinju8”
},
{
“appName”: “玖祥蓝天科技(北京)有限公司”,
“enterprisePhone”: “15910229905”,
“id”: 772814,
“mobile”: “15910229905”,
“userName”: “jiayoubei2018”
},
{
“appName”: “央联万贸城集团有限公司”,
“enterprisePhone”: “13210278898”,
“id”: 772815,
“mobile”: “18653260155”,
“userName”: “ballon”
}
]
使用JSONObject.toJSONString
[
{
“appName”: “中粮人保”,
“enterprisePhone”: “15521293437”,
“id”: 2,
“mobile”: “15521293437”,
“userName”: “blackunique”
},
{
“appName”: “品多多”,
“enterprisePhone”: “”,
“id”: 3,
“mobile”: “13211111551”,
“userName”: “张三132”
},
{
“appName”: “阳光甄选”,
“enterprisePhone”: “13577777778”,
“id”: 4,
“mobile”: “13577777777”,
“userName”: “李四789”
},
{
“appName”: “测试应用”,
“enterprisePhone”: “”,
“id”: 5,
“mobile”: “13537527750”,
“userName”: “huhaiping”
},
{
“appName”: “特奢汇”,
“enterprisePhone”: “”,
“id”: 6,
“mobile”: “13123456123”,
“userName”: “cece002”
},
{
“appName”: “壹分付”,
“enterprisePhone”: “13412124214”,
“id”: 7,
“mobile”: “13156464435”,
“userName”: “cece003”
}
]
但是会报异常
JSONObject.toJSONString源码实现
public static final String toJSONString(Object object) {
return toJSONString(object, new SerializerFeature[0]); }
JSON.toJSON源码实现
json.toJson是
public static final String toJSONString(Object object) { return toJSONString(object, new SerializerFeature[0]); } public static final String toJSONString(Object object, SerializerFeature... features) { SerializeWriter out = new SerializeWriter(); try { JSONSerializer serializer = new JSONSerializer(out); for (com.alibaba.fastjson.serializer.SerializerFeature feature : features) { serializer.config(feature, true); } serializer.write(object); return out.toString(); } finally { out.close(); } }
JSONSerializer类是实现转换javaObject对象的类
/*
* Copyright 1999-2101 Alibaba Group.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.fastjson.serializer;
import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.sql.Clob;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Enumeration;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONAware;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONStreamAware;
import com.alibaba.fastjson.util.ServiceLoader;
/**
* @author wenshao<szujobs@hotmail.com>
*/
public class JSONSerializer {
private final SerializeConfig config;
private final SerializeWriter out;
private List<BeforeFilter> beforeFilters = null;
private List<AfterFilter> afterFilters = null;
private List<PropertyFilter> propertyFilters = null;
private List<ValueFilter> valueFilters = null;
private List<NameFilter> nameFilters = null;
private List<PropertyPreFilter> propertyPreFilters = null;
private int indentCount = 0;
private String indent = "\t";
private String dateFormatPattern;
private DateFormat dateFormat;
private IdentityHashMap<Object, SerialContext> references = null;
private SerialContext context;
public JSONSerializer(){
this(new SerializeWriter(), SerializeConfig.getGlobalInstance());
}
public JSONSerializer(SerializeWriter out){
this(out, SerializeConfig.getGlobalInstance());
}
public JSONSerializer(SerializeConfig config){
this(new SerializeWriter(), config);
}
@Deprecated
public JSONSerializer(JSONSerializerMap mapping){
this(new SerializeWriter(), mapping);
}
public JSONSerializer(SerializeWriter out, SerializeConfig config){
this.out = out;
this.config = config;
}
public String getDateFormatPattern() {
if (dateFormat instanceof SimpleDateFormat) {
return ((SimpleDateFormat) dateFormat).toPattern();
}
return dateFormatPattern;