In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "Java objects and Json text how to achieve conversion", in daily operation, I believe many people in Java objects and Json text how to achieve conversion problems have doubts, small series consulted all kinds of information, sorted out simple and easy to use operation methods, hope to answer "Java objects and Json text how to achieve conversion" doubts helpful! Next, please follow the small series to learn together!
Json is a tool class for converting Java objects and Json text to and from each other.
Install and download the source code git clone https://github.com/njdi/durian.git compile source code cd durian/
Switch to the latest version (Tag), such as: 0.4,
git checkout 0.4
Compile and install to local Maven repository:
mvn clean package add dependencies
To use Config for SpringBoot apps, you need to add:
io.njdi durian-common ${version}
${version} is replaced with a specific version number, such as 0.4.
Java objects
Java objects support the following types: primitive data types, arrays, lists, dictionaries.
basic data types
Integer/int
Long/long
Float/float
Double/double
Boolean/boolean
String
array
int[]
long[]
float[]
double[]
boolean[]
String[]
list
List
List
List
List
List
List
List
dictionary
Map
Map
Map
Map
Map
Map
Map
class
Class
Java objects converted to Json text
Java objects can be converted to Json text using the following methods:
Json.toJson(obj) Basic data type String json;json = Json.toJson(1);json = Json.toJson (1.0);json = Json.toJson(true);json = Json.toJson("str"); array String json;json = Json.toJson (new int[]{1, 2, 3});json = Json.toJson (new double[]{1.0, 2.0, 3.0});json = Json.toJson (new boolean[]{true, false});json = Json.toJson (new String[]{"1", "2.0", "true"}); List String json;json = Json.toJson (List.of(1, 2, 3));json = Json.toJson (List.of(1.0, 2.0, 3.0));json = Json.toJson (List.of(true, false));json = Json.toJson (List.of("1", "2.0", "true")); Dictionary String json;json = Json.toJson (Map.of("key1", 1, "key2", 2));json = Json.toJson (Map.of("key1", 1.0, "key2", 2.0));json = Json.toJson (Map.of("key1", true, "key2", false));json = Json.toJson (Map.of("key1", "str", "key2", "2.0")); class public static class MyObject { private int a; private double b; private boolean c; private String d;}MyObject object = new MyObject();object.a = 1;object.b = 2.0;object.c = true;object.d = "str";String json = Json.toJson(object);Json Text to Java object
When converting Json text to Java object, you need to specify the type information of the converted object through parameters:
Json.fromJson(json, Class)
or
Json.fromJson(json, Type) Basic data type
When converting Json text to basic data types, you need to specify the conversion type with Type, such as:
int a = Json.fromJson("1", Json.JsonType.INT);
Correspondence between basic data types and types:
Integer/int -> Json.JsonType.INT
Long/long -> Json.JsonType.LONG
Float/float -> Json.JsonType.FLOAT
Double/double -> Json.JsonType.DOUBLE
Boolean/boolean -> Json.JsonType.BOOLEAN
String -> Json.JsonType.STRING
array
When converting a Json text to an array, you need to use Type to specify the element type of the converted array, such as:
int[] arr = Json.fromJson("[1, 2, 3]", Json.JsonType.INT_ARRAY);
Correspondence between element type and Type of array:
int[] -> Json.JsonType.INT_ARRAY
long[] -> Json.JsonType.LONG_ARRAY
float[] -> Json.JsonType.FLOAT_ARRAY
double[] -> Json.JsonType.DOUBLE_ARRAY
boolean[] -> Json.JsonType.BOOLEAN_ARRAY
String[] -> Json.JsonType.STRING_ARRAY
list
When converting a Json text to a list, you need to use Type to specify the element type of the conversion list, such as:
List list = Json.fromJson("[1, 2, 3]", Json.JsonType.INT_LIST);
Correspondence between element type and Type of list:
List -> Json.JsonType.INT_LIST
List -> Json.JsonType.LONG_LIST
List -> Json.JsonType.FLOAT_LIST
List -> Json.JsonType.DOUBLE_LIST
List -> Json.JsonType.BOOLEAN_LIST
List -> Json.JsonType.STRING_LIST
List
dictionary
When converting Json text to dictionary, you need to use Type to specify the element value type of the conversion dictionary, such as:
Map map = Json.fromJson("{'a': 1, 'b': 2}", Json.JsonType.INT_MAP);
Map -> Json.JsonType.INT_MAP
Map -> Json.JsonType.LONG_MAP
Map -> Json.JsonType.FLOAT_MAP
Map -> Json.JsonType.DOUBLE_MAP
Map -> Json.JsonType.BOOLEAN_MAP
Map -> Json.JsonType.STRING_MAP
Map -> Json.JsonType.OBJECT_MAP
class
When converting a Json text to a class object, you need to use Class to specify the type of the converted object, such as:
MyObject obj = Json.fromJson("{'a': 1, 'b': 2.0, 'c': true, d: 'str'}", MyObject.class); This concludes the study of "how to convert Java objects and Json texts", hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.