Serializes Java object into JSON string using JSON.toJSONString() with Fastjson
Java Code Examples for com.alibaba.fastjson.JSON.toJSONString()
This method to serializes the specified Java object into JSON string.
Adding Fastjson dependency into your project
Using Gradle
compile group: 'com.alibaba', name: 'fastjson', version: '1.2.56'
Using Maven
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
</dependency>
How to use com.alibaba.fastjson.JSON.toJSONString()
package simplesolution.dev;
import com.alibaba.fastjson.JSON;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JSONToJSONStringExamples {
public static void main(String... args) {
List<Map<String, String>> data = new ArrayList<>();
Map<String, String> item1 = new HashMap<>();
item1.put("name", "Sample JSON Serialization");
item1.put("url", "https://simplesolution.dev");
data.add(item1);
Map<String, String> item2 = new HashMap<>();
item2.put("name", "Java Tutorials");
item2.put("url", "https://simplesolution.dev/java");
data.add(item2);
String jsonStringFromObject = JSON.toJSONString(data);
System.out.println("JSON String from Object: " + jsonStringFromObject);
}
}
Happy Coding 😊