Code to use when you want to process Json with only standard library in Java

point There is a JavaScript engine in the library that comes with the JVM, so use that.

[Addition] ↓ There is an improved version here. Code used when you want to process Json with only the standard library in Java (improved version) gson unnecessary https://qiita.com/oyahiroki/items/006b3511fc4136d02ad1

package script;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class JsonUtil {

	public static Object get(String json, String code) {
		// Get the JavaScript engine
		ScriptEngineManager manager = new ScriptEngineManager();
		ScriptEngine engine = manager.getEngineByName("JavaScript");
		String script = "var obj = " + json + ";";
		try {
			engine.eval(script);
			{
				return engine.eval("obj." + code);
			}
		} catch (ScriptException e) {
			e.printStackTrace();
			return null;
		}
	}
	//How to use/ How to use
	public static void main(String[] args) {
		String json = "{'test':'this is test','test2':{'test3':'value3'}}";

		{
			Object value = JsonUtil.get(json, "test");
			System.out.println(value);
		}
		{
			Object value = JsonUtil.get(json, "test2.test3");
			System.out.println(value);
		}

	}

}


Output result

this is test
value3

Recommended Posts

Code to use when you want to process Json with only standard library in Java
Code used when you want to process Json with only the standard library in Java (improved version) gson unnecessary
When you want to implement Java library testing in Spock with multi-module in Gradle in Android Studio 3
When you want to dynamically replace Annotation in Java8
Use JLine when you want to handle keystrokes on the console character by character in Java
[Java] Use cryptography in the standard library
Code to escape a JSON string in Java
When you want to bind InputStream in JDBI3
I want to use java8 forEach with index
A note when you want Tuple in Java
When you want to use the method outside
[Swift] Use nonzeroBitCount when you want popcnt in Swift
The first thing to do when you want to be happy with Heroku on GitHub with Eclipse in Java
How to use Z3 library in Scala with Eclipse
I want to use ES2015 in Java too! → (´ ・ ω ・ `)
[Java] How to encrypt with AES encryption with standard library
If you want to use Mockito with Kotlin, use mockito-kotlin
How to use JDD library in Scala with Eclipse
Summary of how to use the proxy set in IE when connecting with Java
How to use JSON data in WebSocket communication (Java, JavaScript)
When you want to explicitly write OR or AND with ransack
Things to be aware of when writing code in Java
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
docker-compose.yml when you want to keep mysql running with docker
lombok.config when you want to pass @Qualifier to @RequiredArgsConstructor with lombok
Delegate is convenient to use when you want to reuse parts
[Java] I want to test standard input & standard output with JUnit
[Ruby + Rails] When you want to register in Mailchimp's mail list together with user registration
How to write in Model class when you want to save binary data in DB with PlayFramework
ProxyFactory is convenient when you want to test AOP in Spring!
Use Microsoft Graph with standard Java
Settings to delete unused Java imports when saving with VS Code
How to use classes in Java?
[Java] I want to perform distinct with the key in the object
Do you use Stream in Java?
[Words spelled to me when I was in the first grade ⑦] What I want you to include at least with the Visual Studio Code extension
[RSpec] When you want to use the instance variable of the controller in the test [assigns is not recommended]
Sample code to serialize and deserialize Java Enum enums and JSON in Jackson
When you want Rails to disable a session for a specific controller only
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
Summary of means when you want to communicate with HTTP on Android
(Limited to Java 7 or later) I want you to compare objects in Objects.equals
I want to get the IP address when connecting to Wi-Fi in Java
I want to ForEach an array with a Lambda expression in Java
Multilingual Locale in Java How to use Locale
I want to use DBViewer with Eclipse 2018-12! !!
[JaCoCo (Java Code Coverage)] Use with NetBeans
Working with huge JSON in Java Lambda
I want to use @Autowired in Servlet
Try adding text to an image in Scala using the Java standard library
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (Javassist second decoction)
You may not want to use the remove method in ArrayList very often
When you want to notify an error somewhere when using graphql-spring-boot in Spring Boot
I want you to use Scala as Better Java for the time being
Summary when trying to use Solr in Java and getting an error (Solr 6.x)
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (black magic edition)
I want to send an email in Java.
Static code analysis with Checkstyle in Java + Gradle
I want to use arrow notation in Ruby
With Tomcat you can use placeholders ($ {...}) in web.xml
You use context to use MDC with Spring WebFlux