目录
三、如何实现 将 string存储的 json 转换为键值对形式
一、什么是JSON
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,常用于在不同系统之间传递和存储数据。它使用简洁的文本格式来表示结构化数据,易于阅读和编写,并且可以通过多种编程语言进行解析和生成。
JSON由键值对组成,使用大括号 {} 表示对象,对象中的键和值之间使用冒号 : 分隔,键值对之间使用逗号 , 分隔。值可以是字符串、数字、布尔值、数组、对象或者null。下面是一个简单的JSON示例:
{
  "name": "LoneRanger",
  "age": 23,
  "city": "xxx",
  "isStudent": false,
  "hobbies": ["coding", "swimming", "traveling"],
  "address": {
    "street": "123",
    "city": "xxx",
    "country": "CN"
  }
}
二、String转json,json转String
要将字符串转换为JSON对象,你可以使用各种JSON库中提供的方法。以下是使用Jackson、Gson和JSON.simple这三个常见的JSON库进行示例的代码:
1、使用Jackson库:
String jsonString = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
     
  ObjectMapper objectMapper = new ObjectMapper();
  Object json = objectMapper.readValue(jsonString, Object.class);
2、使用Gson库:
 String jsonString = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
        Gson gson = new Gson();
        Object json = gson.fromJson(jsonString, Object.class);
        // 输出JSON对象
        System.out.println(json);3、使用JSON.simple库:
  String jsonString = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
   JSONParser parser = new JSONParser();
   JSONObject json = (JSONObject) parser.parse(jsonString);
 // 输出JSON对象
   System.out.println(json);4、使用 Fastjson
使用Fastjson库,你可以很方便地将字符串和JSON相互转换。下面是使用Fastjson库进行字符串和JSON之间转换的示例代码:
1、将字符串转换为JSON对象
  JSONObject json = JSON.parseObject(jsonString);在上面的代码中,我们使用JSON.parseObject()方法将字符串解析为Fastjson的 JSONObject对象。
2、将JSON对象转换为字符串:
JSONObject json = new JSONObject();
        json.put("name", "John");
        json.put("age", 30);
        json.put("city", "New York");
        String jsonString = JSON.toJSONString(json);在上面的代码中,我们使用JSON.toJSONString()方法将Fastjson的JSONObject对象转换为字符串。
三、如何实现 将 string存储的 json 转换为键值对形式
要在Java中将存储为字符串的JSON转换为键值对形式,你可以使用 JSON库,例如Jackson、Gson或JSON.simple。下面是使用这些库的示例代码:
1、使用Jackson库:
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class JsonToMapConverter {
    public static void main(String[] args) {
        String jsonString = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode jsonNode = objectMapper.readTree(jsonString);
            Map<String, Object> keyValueMap = new HashMap<>();
            Iterator<Map.Entry<String, JsonNode>> fields = jsonNode.fields();
            while (fields.hasNext()) {
                Map.Entry<String, JsonNode> entry = fields.next();
                String key = entry.getKey();
                JsonNode valueNode = entry.getValue();
                Object value = objectMapper.convertValue(valueNode, Object.class);
                keyValueMap.put(key, value);
            }
            // 遍历键值对
            for (Map.Entry<String, Object> entry : keyValueMap.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();
                System.out.println(key + " = " + value);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
另一种 jackson 方式
String json = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
      
            ObjectMapper objectMapper = new ObjectMapper();
            Map<String, Object> keyValueMap = objectMapper.readValue(json, new TypeReference<Map<String, Object>>() {});
            // 遍历键值对
            for (Map.Entry<String, Object> entry : keyValueMap.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();
                System.out.println("Key: " + key);
                System.out.println("Value: " + value);
            }        在上述代码中,首先定义了一个JSON字符串。然后,使用ObjectMapper类的readValue方法将JSON字符串转换为Map<String, Object>对象。通过遍历这个Map对象的键值对,可以获取键和对应的值,并进行相应的处理。
        需要注意的是,使用readValue方法时,需要通过TypeReference类指定转换的目标类型。在本例中,我们指定了Map<String, Object>作为目标类型。
这样,JSON字符串就可以转换为键值对形式的数据,可以根据具体的需求进一步处理和操作键值对。
2、使用Gson库:
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.Map;
public class JsonToMapConverter {
    public static void main(String[] args) {
        String jsonString = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
        Gson gson = new Gson();
        TypeToken<Map<String, Object>> typeToken = new TypeToken<Map<String, Object>>() {};
        Map<String, Object> keyValueMap = gson.fromJson(jsonString, typeToken.getType());
        // 遍历键值对
        for (Map.Entry<String, Object> entry : keyValueMap.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            System.out.println(key + " = " + value);
        }
    }
}
3、使用JSON.simple库:
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.util.HashMap;
import java.util.Map;
public class JsonToMapConverter {
    public static void main(String[] args) {
        String jsonString = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}";
        JSONParser parser = new JSONParser();
        try {
            JSONObject jsonObject = (JSONObject) parser.parse(jsonString);
            Map<String, Object> keyValueMap = new HashMap<>(jsonObject);
            // 遍历键值对
            for (Map.Entry<String, Object> entry : keyValueMap.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();
                System.out.println(key + " = " + value);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}










