前言

Jsonp用来调用http请求的,非常小巧而且方便

回顾之前所学

二者依赖如下

<!-- jsoup包依赖 -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.1</version>
</dependency>

<!--json-->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>

1、JSONObject 和 JSONArray

1.1、JSONArray

JSONArray jsonArray = JSONArray.fromObject(channelJsonString);
List<ChannelJson> channelJsons = new ArrayList<>();
for (int i =0; i < jsonArray.size(); i++) {
ObjectMapper objectMapper = new ObjectMapper();
try {
ChannelJson channelJson = objectMapper.readValue(jsonArray.get(i).toString(),ChannelJson.class);
if(channelJson.getValue()!=Integer.parseInt(channelListsEnableValue)){
channelJson.setChecked(false);
channelJson.setWeight(0);
}
channelJsons.add(channelJson);

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

}

skinAppInfoCheck.setChannelJson(JSONArray.fromObject(channelJsons).toString());

1.2、JSONObject

String id = jsonObject.put("id");

@RequestMapping("query/uuid/packages")
public void findUuidPackages(String uuid, HttpServletResponse response) throws Exception {

JSONArray jsonArray = new JSONArray();


for (OSSObjectSummary summary : summaryList) {
String bundleId = StringUtils.substringAfter(summary.getKey(),"-");

JSONObject jsonObject = new JSONObject();
jsonObject.put("bundleId",bundleId);
jsonArray.put(jsonObject);
}
}
response.getWriter().write(jsonArray.toString());
}

3、JsonUtils的使用

1、转化成对象

/**
* JsonUtils.toObject
*/
public static ZbtUser getUser(String json

ZbtUser info = JsonUtils.toObject(json, ZbtUser.class);

return info;
}
package com.hlj.ddkj.Jsonp;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.type.TypeFactory;
import org.springframework.util.Assert;

import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Type;

/**
* Utils - JSON
*/
public final class JsonUtils {

/**
* ObjectMapper
*/
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

static {
// jackson 1.9 and before
//objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// or jackson 2.0
OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}

public static ObjectMapper getObjectMapper() {
return OBJECT_MAPPER;
}

/**
* 不可实例化
*/
private JsonUtils() {
}

/**
* 将对象转换为JSON字符串
*
* @param value 对象
* @return JSON字符串
*/
public static String toJson(Object value) {
Assert.notNull(value, "json 不允许为空");
try {
return OBJECT_MAPPER.writeValueAsString(value);
} catch (JsonProcessingException e) {
throw new RuntimeException(e.getMessage(), e);
}
}

/**
* 将JSON字符串转换为对象
*
* @param json JSON字符串
* @param valueType 类型
* @return 对象
*/
public static <T> T toObject(String json, Class<T> valueType) {
Assert.hasText(json, "json 不允许为空");
Assert.notNull(valueType, "valueType 不允许为空");
try {
return OBJECT_MAPPER.readValue(json, valueType);
} catch (JsonParseException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (JsonMappingException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}

/**
* 将JSON字符串转换为对象
*
* @param json JSON字符串
* @param typeReference 类型
* @return 对象
*/
public static <T> T toObject(String json, TypeReference<?> typeReference) {
Assert.hasText(json, "json 不允许为空");
Assert.notNull(typeReference, "typeReference 不允许为空");
try {
return OBJECT_MAPPER.readValue(json, typeReference);
} catch (JsonParseException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (JsonMappingException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}

/**
* 将JSON字符串转换为对象
*
* @param json JSON字符串
* @param javaType 类型
* @return 对象
*/
public static <T> T toObject(String json, JavaType javaType) {
Assert.hasText(json, "json 不允许为空");
Assert.notNull(javaType, "javaType 不允许为空");
try {
return OBJECT_MAPPER.readValue(json, javaType);
} catch (JsonParseException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (JsonMappingException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}

/**
* 将JSON字符串转换为树
*
* @param json JSON字符串
* @return 树
*/
public static JsonNode toTree(String json) {
Assert.hasText(json, "json 不允许为空");
try {
return OBJECT_MAPPER.readTree(json);
} catch (JsonProcessingException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}

/**
* 将对象转换为JSON流
*
* @param writer Writer
* @param value 对象
*/
public static void writeValue(Writer writer, Object value) {
Assert.notNull(writer, "writer 不允许为空");
Assert.notNull(value, "value 不允许为空");
try {
OBJECT_MAPPER.writeValue(writer, value);
} catch (JsonGenerationException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (JsonMappingException e) {
throw new RuntimeException(e.getMessage(), e);
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}

/**
* 构造类型
*
* @param type 类型
* @return 类型
*/
public static JavaType constructType(Type type) {
Assert.notNull(type, "type 不允许为空");
return TypeFactory.defaultInstance().constructType(type);
}

/**
* 构造类型
*
* @param typeReference 类型
* @return 类型
*/
public static JavaType constructType(TypeReference<?> typeReference) {
Assert.notNull(typeReference, "typeReference 不允许为空");
return TypeFactory.defaultInstance().constructType(typeReference);
}

}

2、Jsonp


import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.junit.Test;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class JsonpMain {

@Test
public void testGet() throws IOException {

//data中可以放入我们想要的参数,map类型或者是其他的自己看
//method()后面没有其他方法
Map<String,String> data = new HashMap<>();
Map<String,String> headers = new HashMap<>();

Connection.Response connection = Jsoup.connect("http://www.baidu.com")
.data(data)
.headers(headers)
.method(Connection.Method.GET)
.ignoreContentType(true)
.validateTLSCertificates(false)
.execute();

String body = connection.body();
log.info(body);
}
}

JsonUtils工具类和Jsonp发起http请求_java