
1. Fastjson 如何将 jsonObject 转换成 Map<Integer,Long>类型
import com.alibaba.fastjson.JSONObject;
import java.util.Map;public class Example {public static void main(String[] args) {String jsonStr = "{\\"1\\":100,\\"2\\":200,\\"3\\":300}";JSONObject jsonObject = JSONObject.parseObject(jsonStr);Map<Integer, Long> map = jsonObject.toJavaObject(Map.class);System.out.println(map);}
}
2. Fastjson 如何将 String 转换成 Map<Integer,Long>类型
import com.alibaba.fastjson.JSON;
import java.util.Map;public class Example {public static void main(String[] args) {String jsonStr = "{\\"1\\":100,\\"2\\":200,\\"3\\":300}";Map<Integer, Long> map = JSON.parseObject(jsonStr, Map.class);System.out.println(map);}
}
3. Fastjson 如何将 String 转换成 Json类型
import com.alibaba.fastjson.JSONObject;public class Example {public static void main(String[] args) {String jsonStr = "{\\"name\\":\\"John\\",\\"age\\":30}";JSONObject jsonObject = JSONObject.parseObject(jsonStr);System.out.println(jsonObject);}
}
4. Fastjson 如何将 Json转换成 String类型
import com.alibaba.fastjson.JSONObject;public class Example {public static void main(String[] args) {JSONObject jsonObject = new JSONObject();jsonObject.put("name", "John");jsonObject.put("age", 30);String jsonStr = jsonObject.toJSONString();System.out.println(jsonStr);}
}
5. Fastjson提供的方法来遍历JSONObject对象中的键值对
import com.alibaba.fastjson.JSONObject;public class Example {public static void main(String[] args) {JSONObject jsonObject = new JSONObject();jsonObject.put("name", "John");jsonObject.put("age", 30);jsonObject.put("gender", "male");for (String key : jsonObject.keySet()) {Object value = jsonObject.get(key);System.out.println(key + ": " + value);}}
}