目录
方法2:使用Jackson库将字符串转换为JsonNode对象:
方法2:使用Jackson库将字符串转换为JsonNode对象:
数据转换:
String 转 JSON :
String result = "{\n" +
" \"code\": 200,\n" +
" \"message\": \"查询成功\",\n" +
" \"result\": {\n" +
" \"records\": [\n" +
" {\n" +
" \"jobName\": \"高级开发工程师\",\n" +
" \"jobExperience\": \"一年以下\",\n" +
" \"education\": \"本科\"\n" +
" },\n" +
" {\n" +
" \"jobName\": \"高级开发工程师\",\n" +
" \"jobExperience\": \"一年以下\",\n" +
" \"education\": \"本科\"\n" +
" }\n" +
" ],\n" +
" \"total\": 1,\n" +
" \"size\": 10,\n" +
" \"current\": 1,\n" +
" \"pages\": 1\n" +
" }\n" +
"}";
JSONObject jsonObject = JSON.parseObject(result);
JSON 转 String:
String result = "{\n" +
" \"code\": 200,\n" +
" \"message\": \"查询成功\",\n" +
" \"result\": {\n" +
" \"records\": [\n" +
" {\n" +
" \"jobName\": \"高级开发工程师\",\n" +
" \"jobExperience\": \"一年以下\",\n" +
" \"education\": \"本科\"\n" +
" },\n" +
" {\n" +
" \"jobName\": \"高级开发工程师\",\n" +
" \"jobExperience\": \"一年以下\",\n" +
" \"education\": \"本科\"\n" +
" }\n" +
" ],\n" +
" \"total\": 1,\n" +
" \"size\": 10,\n" +
" \"current\": 1,\n" +
" \"pages\": 1\n" +
" }\n" +
"}";
try {
JSONObject jsonObject = JSON.parseObject(result);
String result2 = JSON.toJSONString(jsonObject);
JSON里的数组获取
方法1:使用JSONbject类。
public static void main(String[] args) {
String result = "{\n" +
" \"code\": 200,\n" +
" \"message\": \"查询成功\",\n" +
" \"result\": {\n" +
" \"records\": [\n" +
" {\n" +
" \"jobName\": \"高级开发工程师\",\n" +
" \"jobExperience\": \"一年以下\",\n" +
" \"education\": \"本科\"\n" +
" },\n" +
" {\n" +
" \"jobName\": \"高级开发工程师\",\n" +
" \"jobExperience\": \"一年以下\",\n" +
" \"education\": \"本科\"\n" +
" }\n" +
" ],\n" +
" \"total\": 1,\n" +
" \"size\": 10,\n" +
" \"current\": 1,\n" +
" \"pages\": 1\n" +
" }\n" +
"}";
try {
JSONObject jsonObject = new JSONObject(result);
JSONObject resultObject = jsonObject.getJSONObject("result");
JSONArray recordsArray = resultObject.getJSONArray("records");
for (int i = 0; i < recordsArray.size(); i++) {
JSONObject recordObject = recordsArray.getJSONObject(i);
String jobName = recordObject.getString("jobName");
String jobExperience = recordObject.getString("jobExperience");
String education = recordObject.getString("education");
System.out.println("Job Name: " + jobName);
System.out.println("Job Experience: " + jobExperience);
System.out.println("Education: " + education);
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
}
}
方法2:使用Jackson库将字符串转换为JsonNode对象:
public static void main(String[] args) {
String result = "{\n" +
" \"code\": 200,\n" +
" \"message\": \"查询成功\",\n" +
" \"result\": {\n" +
" \"records\": [\n" +
" {\n" +
" \"jobName\": \"高级开发工程师\",\n" +
" \"jobExperience\": \"一年以下\",\n" +
" \"education\": \"本科\"\n" +
" },\n" +
" {\n" +
" \"jobName\": \"高级开发工程师\",\n" +
" \"jobExperience\": \"一年以下\",\n" +
" \"education\": \"本科\"\n" +
" }\n" +
" ],\n" +
" \"total\": 1,\n" +
" \"size\": 10,\n" +
" \"current\": 1,\n" +
" \"pages\": 1\n" +
" }\n" +
"}";
try {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(result);
JsonNode recordsNode = jsonNode.get("result").get("records");
for (JsonNode recordNode : recordsNode) {
String jobName = recordNode.get("jobName").asText();
String jobExperience = recordNode.get("jobExperience").asText();
String education = recordNode.get("education").asText();
System.out.println("Job Name: " + jobName);
System.out.println("Job Experience: " + jobExperience);
System.out.println("Education: " + education);
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
}
}
JSON里的对象获取
方法1:使用JSONbject类。
public class Main {
public static void main(String[] args) {
String jsonString = "{\"name\": \"John\", \"age\": 30}";
try {
JSONObject jsonObject = new JSONObject(jsonString);
String name = jsonObject.getString("name");
int age = jsonObject.getInt("age");
System.out.println("Name: " + name); // 输出: John
System.out.println("Age: " + age); // 输出: 30
} catch (Exception e) {
e.printStackTrace();
}
}
}
方法2:使用Jackson库将字符串转换为JsonNode对象:
public class Main {
public static void main(String[] args) {
String jsonString = "{\"name\": \"John\", \"age\": 30}";
try {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(jsonString);
// 可以使用jsonNode进行进一步的处理
System.out.println(jsonNode.get("name").asText()); // 输出: John
System.out.println(jsonNode.get("age").asInt()); // 输出: 30
} catch (Exception e) {
e.printStackTrace();
}
}
}