search
HomeJavaJavaBaseHow to determine whether a string is in json format in java

How to determine whether a string is in json format in java

Nov 21, 2019 pm 02:44 PM
javajsonjudgmentstringFormat

How to determine whether a string is in json format in java

#1. Simply judge whether it is in json format. Judgment rules: judge whether the first and last letters are {} or []. If they are neither, it is not a text in JSON format.

The code is implemented as follows:

public static boolean getJSONType(String str) {
	boolean result = false;
	if (StringUtils.isNotBlank(str)) {
		str = str.trim();
		if (str.startsWith("{") && str.endsWith("}")) {
			result = true;
		} else if (str.startsWith("[") && str.endsWith("]")) {
			result = true;
		}
	}
	return result;
}

2, Judged by fastjson parsing, if the parsing is successful, it is json format; otherwise, it is not json format

The code is implemented as follows:

public static boolean isJSON2(String str) {
	boolean result = false;
	try {
		Object obj=JSON.parse(str);
		result = true;
	} catch (Exception e) {
		result=false;
	}
	return result;
}

Recommended tutorial: java introductory tutorial

The above is the detailed content of How to determine whether a string is in json format in java. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft