1.导入外部sdk:
转换目前来说都是借用阿里巴巴的fastjson工具类来转换的,也可使用google的Gson sdk,但是我的华为手机用不了外国滴东西(哈哈,说笑了,但是极其推荐fastjson,其他的sdk总有一些奇葩问题)
这里我们引入sdk,如下
implementation ‘com.alibaba:fastjson:1.2.62’ 点击sync就引入成功了;
2.JSONObject、Json字符串,实体对象,JSONArray的含义:
a.JSONObject:它是一种数据结构,一种JSON格式的数据结构(key-value结构),可以使用put函数给jsonobject对象添加元素。
怎样得到json数据格式:
b.json字符串的含义:json字符串在java中通常的表现形式是:
{"name":"sjx","age":24,"height":160}
开发中发现这个json字符串本质上还是一个jsonobject对象,并不是真正意义上的json字符串(即使将jsonobject对象转字符串),但是网络请求传输中用到json格式的字符串(json字符串)都是这种形式的,
测试代码:
输出jsonobject对象、输出jsonobject对象转字符串
public class JavaMainMethodTest {
public static void main(String[] args) {
// JsonConvertUtils.INSTANCE.JSONObjectToString();
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "sjx");
jsonObject.put("age", 25);
jsonObject.put("height", 170);
System.out.println(jsonObject);
System.out.println(jsonObject.toString());
}
}
输出结果:
{"name":"sjx","age":25,"height":170} //json对象打印
{"name":"sjx","age":25,"height":170} //json对象toString打印
c.实体对象:java对象,例如:
Coder coder = new Coder();
coder就是一给实体对象
d.JSONArray对象:是一个有序的值序列,文本形式就是一个外部方括号的字符串
[{"name":"sjx","age":24,"height":160},{"name":"ls","age":28,"height":170},{"name":"zs","age":34,"height":160}]
3.相互转换的方式
实例化一个对象作为例子:
Student student = new Student("sjx", 24);
1.实体对象转json字符串
String jsonString = JSONObject.toJSONString(student);
2.json字符串转实体对象
Student student = JSONObject.parseObject(jsonString, Student.class)
3.JSONObject对象转json字符串
String jsonString = JSONObject.toJSONString(jsonObject);
4.json字符串转JSONObject对象
JSONObject jsonObject = JSONObject.parseObject(jsonString);
5.JSONObject对象转实体对象
Student student = JSONObject.toJavaObject(jsonObject, Student.class)
6.实体对象转JSONObject对象,需要借用json字符串再转换;
上面的转换举个代码例子吧(生成json数据格式):
防止大段代码贴上来,直接用kotlin了,节省篇幅。
object JsonConvertUtils {
private var coder = Coder("sjx", 17, 170)
fun JSONObjectToString() {
/*the first method*/
val jsonObject = JSONObject()
try {
jsonObject["name"] = "sjx"
jsonObject["age"] = 24
jsonObject["height"] = 160
println(jsonObject)
} catch (e: JSONException) {
e.printStackTrace()
}
/*the second method*/
val map = HashMap<String, Any>()
map["name"] = "sjx"
map["age"] = 24
map["height"] = 160
println(JSONObject(map).toString())
/*the third method*/
coder.name = "sjx"
coder.age = 24
coder.height = 160
/*生成json格式*/println(JSON.toJSON(coder))
/*实体对象转成string*/
val coderString = JSONObject.toJSONString(coder)
/*the fourth method*/
/*JSON字符串converted JSON对象*/
val coderJsonString = "{\"name\":\"sjx\",\"age\":24,\"height\":160}"
val jsonObject2 = JSONObject.parseObject(coderJsonString)
println(jsonObject2.toString())
}
class Coder(var name: String, var age: Int, var height: Int)
}
我们看下标准的json格式,它是一个jsonobject对象,但其实它也是我们在java中常用的json字符串,这么说有点奇怪,可以试着输出一下jsonobject对象和jsonobject.toString发现打印出来的都是一个具有key-value结构的字符串。
4.标准json字符串和工作中常见的json字符串
何为标准json字符串?
答:标准json字符串是指双引号需要使用反斜杠\转义的,外部再使用一个双引号包裹的;
"{\"name\":\"sjx\",\"age\":24,\"height\":160}"
这种带转义字符且用双引号包裹的json字符串在工作中很少用到,一般是那种奇葩需求,后台明明需要一个json对象,却要拿字符串接收,然后服务器再去解析那个标准json字符串,解析成他需要的实体对象,一般这种需求出现在多层实体对象嵌套之中。
何为json字符串?
答:json字符串本质还是一个jsonobject对象,sout一个jsonobject对象打印的结果和sout一个jsonobject.toString()打印出来的结果是一样的。网络请求中我们用来传输的就是这种json字符串,我们发送请求体和接收响应体数据时就是json字符串,之后我们再把他们转成jsonobject对象,最后再转成实体对象。总结就是打印出来jsonobject对象和json字符串虽然看起来一摸一样,但是是两种不同的东西,需要在代码中把json字符串转成jsonobject对象(客户端接收响应时),jsonobject对象转成json字符串(客户端发送请求时);