JSON相关操作

  google包的Gson把对象转换成json字符串往redis进行存储的,取值时,再通过Gson().fromJson()转换成对象。用此功能时时需要引入gson-2.1.jar



jar包 生成对象和对象和JSON字符串的转换

<!-- 格式化对象,方便输出日志 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
</dependency>

<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20131018</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.github.iweinzierl/jsonformat -->
<dependency>
<groupId>com.github.iweinzierl</groupId>
<artifactId>jsonformat</artifactId>
<version>1.0</version>
</dependency>






package com.yiche.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.yiche.entity.DynaUserRetValue;
import com.yiche.entity.FeedDetail;
import com.yiche.entity.Mbran;
import com.yiche.entity.SunMbran;
import com.yiche.entity.request.*;
import com.yiche.entity.response.BasicInfo;
import com.yiche.entity.response.Hotcs;
import com.yiche.message.FeedInfo;
import com.yiche.service.FeedService;
import org.apache.poi.util.SystemOutLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.xml.transform.Result;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class FeedServiceImpl implements FeedService {
@Autowired
private RedisTemplate redisTemplate;

/**
* 根据传入的城市ID等信息获取动态信息
*
* @param jsonRequest
* @return
*/
public DynaUserRetValue getAllInfo(JsonRequest jsonRequest) {
String string = (String) redisTemplate.opsForValue().get("getAllInfo"+"DynaUserRetValue"+jsonRequest);
DynaUserRetValue dynaUserRetValue=null;
if(string!=null){
dynaUserRetValue=JSONObject.parseObject(string,DynaUserRetValue.class);
}else {
String result = null;
try {
result = FeedInfo // http://api.easypass.cn/huoban/feeds/Feed/Search/
.saveDataToInterface(
jsonRequest, "<a href="http://feeds.wfw.yichehuoban.cn/feeds/Feed/Search/" );"="">http://feeds.wfw.yichehuoban.cn/feeds/Feed/Search/");
} catch (Exception e) {
e.printStackTrace();
}

System.out.println(result+".............................................................");
// 将接口调用获取的结果转换成JSON对象
JSONObject jsonObject = JSONObject.parseObject(result);
String object1 = jsonObject.get("result").toString();
System.out.println(object1 + "(结果状态:1正常,不是1异常)");
if ("1".equals(object1)) {//判断获取的结果是否正确
//取出Reyvalue结果,将结果进行循环遍历并将结果封装到泛型位DynaUser的List集合中
JSONObject retValue = JSONObject.parseObject(jsonObject.get("RetValue").toString());
Gson gson = new Gson();
dynaUserRetValue=gson.fromJson(retValue.toString(),DynaUserRetValue.class);
String json = (String) JSON.toJSONString(retValue);
redisTemplate.opsForValue().set("getAllInfo"+"DynaUserRetValue"+jsonRequest,json);
} else {
throw new RuntimeException("系统异常");
}
}
System.out.println(dynaUserRetValue);
return dynaUserRetValue;

}

/**
* 根据用户Id获取所有C端用户的所有feedId
*
* @param mySearchRequest
* @return
*/

public List<String> getFeedIds(MySearchRequest mySearchRequest) {
String string = (String) redisTemplate.opsForValue().get("getFeedIds" + "list" + mySearchRequest);
List<String> feedIds = new ArrayList<String>();
if(string!=null){
JSONObject jsonObject = JSONObject.parseObject(string);
Object dataModelList = jsonObject.get("DataModelList");
JSONArray jsonArray = JSONObject.parseArray(dataModelList.toString());

for (int i=0;i<jsonArray.size();i++){
/*
他写的

String feedId = JSON.toJSONString(jsonArray.get(i).toString());
*/

/*
* 我改的
* */
String feedId=JSONObject.parseObject(jsonArray.get(i).toString()).get("FeedId").toString();

feedIds.add(feedId);
}

return feedIds;
}

String result = null;
try {
result = FeedInfo.saveDataToInterface(
mySearchRequest, "<a href="http://feeds.wfw.yichehuoban.cn/feeds/Feed/CMySearch/" );"="">http://feeds.wfw.yichehuoban.cn/feeds/Feed/CMySearch/");


} catch (Exception e) {
e.printStackTrace();
}
JSONObject jsonObject = JSONObject.parseObject(result);
String result1 = (String) jsonObject.get("result").toString();
System.out.println(result1 + "(结果状态:1正常,非1不正常");
if ("1".equals(result1)) {
//获取DataModelListJSON对象
JSONObject retValue = JSONObject.parseObject(jsonObject.get("RetValue").toString());

Object o = retValue.get("DataModelList");
JSONArray jsonArray = JSONObject.parseArray(o.toString());
for (int i = 0; i < jsonArray.size(); i++) {
String feedId = JSONObject.parseObject(jsonArray.get(i).toString()).get("FeedId").toString();
System.out.println(feedId+"..........");
System.out.println(feedId + "service层getFeedIds方法获取所有C端用户的feedId");
feedIds.add(feedId);
}
String jsonString = JSON.toJSONString(retValue);
redisTemplate.opsForValue().set("getFeedIds" + "list" + mySearchRequest,jsonString);
} else {
throw new RuntimeException("系统异常");
}

return feedIds;
}

/**
* 根据FeedId获取用户的动态信息
*
* @param detailRequest
* @return
*/
public FeedDetail getFeedDetail(DetailRequest detailRequest) {
String string = (String) redisTemplate.opsForValue().get("getFeedDetail" + "FeedDetail" + detailRequest);
System.out.println("detail方法redis中的"+string);
FeedDetail feedDetail = null;
if (string!=null){
feedDetail = JSONObject.parseObject(string, FeedDetail.class);
}else {

String result = null;
try {
result = FeedInfo.saveDataToInterface(detailRequest, "<a href="http://feeds.wfw.yichehuoban.cn/feeds//Feed/Detail/" );"="">http://feeds.wfw.yichehuoban.cn/feeds//Feed/Detail/");
} catch (Exception e) {
e.printStackTrace();
}
JSONObject jsonObject = JSONObject.parseObject(result);
Object o = jsonObject.get("result");
System.out.println(o.toString() + "(结果状态:1正常,非1不正常)");
if ("1".equals(o.toString())) {
Gson gson = new Gson();
feedDetail = gson.fromJson(jsonObject.get("RetValue").toString(), FeedDetail.class);
System.out.println(feedDetail);
System.out.println(feedDetail + "service层getFeedDetail方法获取动态信息");
String string1 = (String)JSON.toJSONString(jsonObject.get("RetValue"));
redisTemplate.opsForValue().set("getFeedDetail" + "FeedDetail" + detailRequest,string1);
} else {
throw new RuntimeException("获取" +
" 信息异常");
}

}
return feedDetail;
}

/**
* 根据城市Id获取当前城市下的主品牌的相关信息
*
* @return
*/
public List<Mbran> getMbran(MbranRequest mbranRequest) throws IOException {
//从redis中获取相关数据
System.out.println(mbranRequest+"#$#$");
String o = (String) redisTemplate.opsForValue().get("getMbran" + "list" + mbranRequest);
List<Mbran> mbrans = new ArrayList<Mbran>();
System.out.println(o);
if (o!=null){
JSONArray objects = JSONObject.parseArray(o);
for (int i=0;i<objects.size();i++){
Mbran mbran = JSONObject.parseObject(objects.get(i).toString(), Mbran.class);
mbrans.add(mbran);
}
}else {
String result = null;
try {
result = FeedInfo.saveDataToInterface(mbranRequest, "<a href="http://feeds.wfw.yichehuoban.cn/feeds/Feed/GetMbrandFeedCount/" );"="">http://feeds.wfw.yichehuoban.cn/feeds/Feed/GetMbrandFeedCount/");
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
JSONObject jsonObject = JSONObject.parseObject(result);
Object result1 = jsonObject.get("result");
System.out.println(result1.toString() + "(结果状态:1正常,非1不正常)");
if ("1".equals(result1.toString())) {
JSONArray retValue = JSONObject.parseArray(jsonObject.get("RetValue").toString());
for (int i = 0; i < retValue.size(); i++) {
Gson gson = new Gson();
System.out.println("获取接口中集合的数据" + retValue.get(i).toString());
JSONObject jsonObject1 = JSONObject.parseObject(retValue.get(i).toString());
Mbran mbran=gson.fromJson(jsonObject1.toString(),Mbran.class);
System.out.println(mbran + "service层getMbran方法获取所有城市主品牌信息");
mbrans.add(mbran);
}
//将获取的结果存放到redis缓存中
String string = JSON.toJSONString(retValue);
System.out.println("即将存到redis的数据"+retValue);
redisTemplate.opsForValue().set("getMbran" + "list" + mbranRequest,string);
} else {
throw new RuntimeException("系统异常");
}
}

return mbrans;
}

/**
* 根据城市Id和主品牌的Id获取该主品牌下的子品牌的相关信息
* @param sunMbranRequest
* @return
*/
public List<SunMbran> getSunMbran(SunMbranRequest sunMbranRequest){
String o = (String) redisTemplate.opsForValue().get("getSunMbran" + "list" + sunMbranRequest);
String result=null;
List<SunMbran> sunMbrans=new ArrayList<SunMbran>();
if (o!=null){
JSONArray objects = JSONObject.parseArray(o);
for (int i=0;i<objects.size();i++){
SunMbran sunMbran = JSONObject.parseObject(objects.get(i).toString(), SunMbran.class);
sunMbrans.add(sunMbran);
}
}else{
try {
result=FeedInfo.saveDataToInterface(sunMbranRequest,"<a href="http://feeds.wfw.yichehuoban.cn/feeds/Feed/GetBrandFeedCount/" );"="">http://feeds.wfw.yichehuoban.cn/feeds/Feed/GetBrandFeedCount/");
} catch (Exception e) {
e.printStackTrace();
}

JSONObject jsonObject = JSONObject.parseObject(result);
Object result1 = jsonObject.get("result");
System.out.println(result1.toString() + "(结果状态:1正常,非1不正常)");
if ("1".equals(result1.toString())) {
JSONArray retValue = JSONObject.parseArray(jsonObject.get("RetValue").toString());
for (int i = 0; i < retValue.size(); i++) {
JSONObject jsonObject1 = JSONObject.parseObject(retValue.get(i).toString());
Gson gson=new Gson();
SunMbran sunMbran=gson.fromJson(jsonObject1.toString(),SunMbran.class);
//feedDetail = gson.fromJson(jsonObject.get("RetValue").toString(), FeedDetail.class);
System.out.println(sunMbran + "service层getSunMbran方法获取所有城市主品牌信息");
sunMbrans.add(sunMbran);
}
String string = JSON.toJSONString(retValue);
redisTemplate.opsForValue().set("getSunMbran" + "list" + sunMbranRequest,string);
} else {
throw new RuntimeException("系统异常");
}
}
System.out.println(sunMbrans);
return sunMbrans;
}

/**
* 获取经销商的相关信息
*
* @param basicInfoRequest
* @return
*/
public BasicInfo getBasicInfo(BasicInfoRequest basicInfoRequest) {
String o = (String) redisTemplate.opsForValue().get("getBasicInfo" + "BasicInfo" + basicInfoRequest);
BasicInfo basicInfo=null;
String result = null;
if (o!=null) {
basicInfo = JSONObject.parseObject(o, BasicInfo.class);
}else{
try {
result = FeedInfo.saveDataToInterface(
basicInfoRequest, "<a href="http://idata.easypass.cn/vendor/basicinfo" );"="">http://idata.easypass.cn/vendor/basicinfo");
} catch (Exception e) {
e.printStackTrace();
}

Gson gson=new Gson();
if(result!=null) {


System.out.println(result);

result=result.replaceFirst("]","");
int i=result.indexOf('[');
result=result.substring(0,i)+result.substring(i+1,result.length());


/*<!-- 我修改的内容 -->*/
try {
basicInfo = gson.fromJson(result.toString(), BasicInfo.class);
}catch (Exception e){
e.printStackTrace();
}



System.out.println(basicInfo+"............");

// basicInfo = gson.fromJson(jsonObject.toString(), BasicInfo.class);


redisTemplate.opsForValue().set("getBasicInfo" + "BasicInfo" + basicInfoRequest,result.toString());
} else{
throw new RuntimeException("暂无经销商信息");
}
}
return basicInfo;
}

/**
* 获取经销商热销车型
* @param hotcsRequest
* @return
*/

public List<Hotcs> getHotcs(HotcsRequest hotcsRequest){
String redis = (String) redisTemplate.opsForValue().get("getHotcs" + "list" + hotcsRequest);
System.out.println(redis);
List<Hotcs> hotcss=new ArrayList<Hotcs>();
Hotcs hotcs=null;
String result=null;
if (redis!=null){
JSONArray jsonArray = JSONObject.parseArray(redis);
for (int i=0;i<jsonArray.size();i++){

hotcs = JSONObject.parseObject(jsonArray.get(i).toString(), Hotcs.class);
hotcss.add(hotcs);
}

return hotcss;
}
try {
result = FeedInfo.saveDataToInterface(
hotcsRequest, "<a href="http://idata.easypass.cn//price/hotcs" );"="">http://idata.easypass.cn//price/hotcs");
} catch (Exception e) {
e.printStackTrace();
}

Gson gson = new Gson();

if (result != null) {
JSONArray jsonArray = JSONObject.parseArray(result);
System.out.println(jsonArray);

for (int i = 0; i < jsonArray.size(); i++) {

Object o = jsonArray.get(i);
// JSONObject jsonObject = JSONObject.parseObject(o.toString(),Hotcs.class);
hotcs= JSONObject.parseObject(o.toString(),Hotcs.class);
//int csId = Integer.parseInt(jsonObject.get("CsId").toString());
//System.out.println(csId+"获取json对象的ID");
//System.out.println("###########################");
//System.out.println(jsonArray.get(i));
//jsonArray.get(i).toString()
/* try {
= gson.fromJson(jsonObject.toJSONString(), Hotcs.class);
}catch (Exception e){
e.printStackTrace();
}*/



//System.out.println(hotcs.getCoverImg());
System.out.println(hotcs + "service层getBasicInfo方法获取动态信息");
hotcss.add(hotcs);
}

String string = JSON.toJSONString(jsonArray);
redisTemplate.opsForValue().set("getHotcs" + "list" + hotcsRequest, string);
} else {
throw new RuntimeException("系统异常");
}

return hotcss;
}

/*
* feed1页面推荐前4名
* */

public List<Integer> GetCityUserCount(CityRequest cityRequest){

String citys = (String) redisTemplate.opsForValue().get("getCitys" + "list" + cityRequest);
System.out.println(citys);
List<Integer> citysList=new ArrayList<Integer>();

String result=null;
if (citys!=null){
JSONArray jsonArray = JSONObject.parseArray(citys);
for (int i=0;i<jsonArray.size();i++){


citysList.add(Integer.parseInt(jsonArray.get(i).toString()));
}

return citysList;
}
try {
result = FeedInfo.saveDataToInterface(
cityRequest, "<a href="http://feeds.wfw.yichehuoban.cn/feeds/Feed/GetCityUserCount/" );"="">http://feeds.wfw.yichehuoban.cn/feeds/Feed/GetCityUserCount/");
} catch (Exception e) {
e.printStackTrace();
}



if (result != null) {



JSONObject jsonObject1=JSONObject.parseObject(result);
JSONObject jsonObject=JSONObject.parseObject(jsonObject1.get("RetValue").toString());

Object retValue=jsonObject.get("UserId");
JSONArray jsonArray=JSONArray.parseArray(retValue.toString());

for (int i = 0; i < jsonArray.size(); i++) {

Object o = jsonArray.get(i);
citysList.add(Integer.parseInt(o.toString()));
}


String string = JSON.toJSONString(jsonArray);
redisTemplate.opsForValue().set("getCitys" + "list" + cityRequest, string);
} else {
throw new RuntimeException("系统异常");
}

return citysList;

}



/*
推荐的每个人的feed总数量

* */
public Integer GetUserFeedCount(UserRequest userRequest){

String feedCount=(String)redisTemplate.opsForValue().get("getCount"+userRequest);
if (feedCount != null){

return Integer.parseInt(feedCount);
}
String result=null;
try {

result=FeedInfo.saveDataToInterface(userRequest,"<a href="http://feeds.wfw.yichehuoban.cn/feeds/Feed/GetUserFeedCount/" );"="">http://feeds.wfw.yichehuoban.cn/feeds/Feed/GetUserFeedCount/");

}catch (Exception e){
e.printStackTrace();
}

String feedId=null;

if (result != null){

JSONObject jsonObject=JSONObject.parseObject(result);

feedId=jsonObject.get("RetValue").toString();
redisTemplate.opsForValue().set("getCount"+userRequest,feedId);

}else {
throw new RuntimeException("得到");
}


return Integer.parseInt(feedId);
}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值