https://github.com/panpf/jsonx
Extensions to the org.json standard library
https://github.com/panpf/jsonx
java json kotlin
Last synced: about 1 year ago
JSON representation
Extensions to the org.json standard library
- Host: GitHub
- URL: https://github.com/panpf/jsonx
- Owner: panpf
- License: apache-2.0
- Archived: true
- Created: 2019-04-28T08:28:57.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2021-05-22T10:15:08.000Z (about 5 years ago)
- Last Synced: 2025-02-24T16:52:38.805Z (over 1 year ago)
- Topics: java, json, kotlin
- Language: Java
- Size: 138 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# JSONX
[![Platform][platform_java_icon]][platform_java_link]
[![Platform][platform_kotlin_icon]][platform_kotlin_link]
![SourceCompatibility][source_compatibility_icon]
![TargetCompatibility][target_compatibility_icon]
![KotlinJvmTarget][kotlin_jvmtarget_icon]
[![License][license_icon]][license_link]
Extensions to the org.json standard library
## Getting Started
This library has been published to `mavenCentral`, Add the following to your `build.gradle` file
```grovvy
implementation "io.github.panpf.jsonx:jsonx:${lastVersion}"
implementation "io.github.panpf.jsonx:jsonx-ktx:${lastVersion}" // Not required. Kotlin extension
implementation "io.json:org.json:2.0" // Not required. You need it if you are running on the java platform
```
`${lastVersion}`: [![Download][release_icon]][release_link] (no include 'v')
Dependencies:
* org.jetbrains:annotations: 13.0
* org.jetbrains.kotlin:kotlin-stdlib-jdk7: 1.4.10(Only the '\*-ktx' library dependencies it)
## Samples
### Empty judgment:
```java
assertTrue(Jsonx.isEmptyJSON(null));
assertTrue(Jsonx.isEmptyJSON(""));
assertTrue(Jsonx.isEmptyJSON("null"));
assertTrue(Jsonx.isEmptyJSON("{}"));
assertTrue(Jsonx.isEmptyJSON("[]"));
assertFalse(Jsonx.isEmptyJSON("{\"key\":\"value\"}"));
```
### conversion:
```
assertEquals("[\"1\",\"2\",\"3\"]", Jsonx.toJSONArray(new String[]{"1", "2", "3"}).toString());
assertArrayEquals(new String[]{"1", "2", "3"}, Jsonx.toStringArray(new JSONArray("[\"1\",\"2\",\"3\"]")));
```
### get/opt method multiple key support:
```
JSONObject jsonObject = new JSONObject();
jsonObject.put("nick", "Tony");
assertEquals("Tony", Jsonx.getWithKeys(jsonObject, new String[]{"nick", "name"}).toString());
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("name", "Tony");
assertEquals("Tony", Jsonx.getWithKeys(jsonObject2, new String[]{"nick", "name"}).toString());
```
### format:
```java
String formatted = Jsonx.formatJSON("{\"age\":20,\"name\":\"David\"}")
System.out.println(formatted)
```
out:
```
{
"age":20,
"name":"David"
}
```
### JSONArray Sequence support in kotlin
```kotlin
val jsonArray = JSONArray("[1, 2, 3, 4]")
jsonArray.asSequence().forEach{
println("item: $it")
}
```
### Method List
check:
* boolean isJSON(String)
* boolean isNotJSON(String)
* boolean isJSONObject(String)
* boolean isNotJSONObject(String)
* boolean isJSONArray(String)
* boolean isNotJSONArray(String)
* boolean isEmptyJSON(String)
* boolean isNotEmptyJSON(String)
* boolean isEmptyJSONObject(String)
* boolean isNotEmptyJSONObject(String)
* boolean isEmptyJSONArray(String)
* boolean isNotEmptyJSONArray(String)
toJsonObject:
* JSONObject toJSONObject(String)
* JSONObject toJSONObjectOrNull(String)
* JSONObject toJSONObject(T, ToJSONObject)
* JSONObject toJSONObjectOrNull(T, ToJSONObjectOrNull)
toJsonArray:
* JSONArray toJSONArray(String)
* JSONArray toJSONArrayOrNull(String)
* JSONArray toJSONArray(List, ToJSONObject)
* JSONArray toJSONArrayOrNull(List, ToJSONObjectOrNull)
* JSONArray toJSONArray(List)
* JSONArray toJSONArrayOrNull(List)
* JSONArray toJSONArray(T[] array, ToJSONObject)
* JSONArray toJSONArrayOrNull(T[] array, ToJSONObjectOrNull)
* JSONArray toJSONArray(T[])
* JSONArray toJSONArrayOrNull(T[])
* JSONArray toJSONArray(int[])
* JSONArray toJSONArrayOrNull(int[])
* JSONArray toJSONArray(double[])
* JSONArray toJSONArrayOrNull(double[])
* JSONArray toJSONArray(long[])
* JSONArray toJSONArrayOrNull(long[])
* JSONArray toJSONArray(boolean[])
* JSONArray toJSONArrayOrNull(boolean[])
toBean:
* Bean toBean(JSONObject, ToBean)
* ArrayList toBeanList(JSONArray, ToBean)
* ArrayList toBeanListOrNull(JSONArray, ToBeanOrNull)
* Bean[] toBeanArray(JSONArray, ToBean)
* Bean[] toBeanArrayOrNull(JSONArray, ToBeanOrNull)
* String[] toStringArray(JSONArray)
* String[] toStringArrayOrNull(JSONArray)
* ArrayList toStringList(JSONArray)
* ArrayList toStringListOrNull(JSONArray)
* int[] toIntArray(JSONArray)
* int[] toIntArrayOrNull(JSONArray)
* double[] toDoubleArray(JSONArray)
* double[] toDoubleArrayOrNull(JSONArray)
* long[] toLongArray(JSONArray)
* long[] toLongArrayOrNull(JSONArray)
* boolean[] toBooleanArray(JSONArray)
* boolean[] toBooleanArrayOrNull(JSONArray)
opt and get:
* Object optWithKeys(JSONObject, String[])
* String optStringWithKeys(JSONObject, String[], String)
* String optStringWithKeys(JSONObject, String[])
* int optIntWithKeys(JSONObject, String[], int)
* int optIntWithKeys(JSONObject, String[])
* long optLongWithKeys(JSONObject, String[], long)
* long optLongWithKeys(JSONObject, String[])
* boolean optBooleanWithKeys(JSONObject, String[], boolean)
* boolean optBooleanWithKeys(JSONObject, String[])
* double optDoubleWithKeys(JSONObject, String[], double)
* double optDoubleWithKeys(JSONObject, String[])
* JSONObject optJSONObjectWithKeys(JSONObject, String[])
* JSONArray optJSONArrayWithKeys(JSONObject, String[])
* Object getWithKeys(JSONObject, String[])
* String getStringWithKeys(JSONObject, String[])
* int getIntWithKeys(JSONObject, String[])
* long getLongWithKeys(JSONObject, String[])
* boolean getBooleanWithKeys(JSONObject, String[])
* double getDoubleWithKeys(JSONObject, String[])
* JSONObject getJSONObjectWithKeys(JSONObject, String[])
* JSONArray getJSONArrayWithKeys(JSONObject, String[])
format:
* String formatToString(JSONObject)
* String formatToStringOrNull(JSONObject)
* String formatToString(JSONArray)
* String formatToStringOrNull(JSONArray)
* String formatJSON(String json)
* String formatJSONOrNull(String json)
More features please refer to the source code [Jsonx.java] ([Test][JsonxTest.java])、[Jsonx.kt] ([Test][JsonxTest.kt])
## Change Log
Please view the [CHANGELOG.md] file
## License
Copyright (C) 2019 Peng fei Pan
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
[platform_java_icon]: https://img.shields.io/badge/Platform-Java-red.svg
[platform_java_link]: https://www.java.com
[platform_kotlin_icon]: https://img.shields.io/badge/Platform-Kotlin-blue.svg
[platform_kotlin_link]: http://kotlinlang.org
[source_compatibility_icon]: https://img.shields.io/badge/SourceCompatibility-1.7-red.svg
[target_compatibility_icon]: https://img.shields.io/badge/TargetCompatibility-1.7-red.svg
[kotlin_jvmtarget_icon]: https://img.shields.io/badge/KotlinJvmTarget-1.6-red.svg
[license_icon]: https://img.shields.io/badge/License-Apache%202-blue.svg
[license_link]: https://www.apache.org/licenses/LICENSE-2.0
[release_icon]: https://img.shields.io/maven-central/v/io.github.panpf.jsonx/jsonx
[release_link]: https://repo1.maven.org/maven2/io/github/panpf/jsonx/
[Jsonx.java]: jsonx/src/main/java/com/github/panpf/jsonx/Jsonx.java
[JsonxTest.java]: jsonx/src/test/java/com/github/panpf/jsonx/test/JsonxTest.java
[Jsonx.kt]: jsonx-ktx/src/main/java/com/github/panpf/jsonx/Jsonx.kt
[JsonxTest.kt]: jsonx-ktx/src/test/java/com/github/panpf/jsonx/test/JsonxTest.kt
[CHANGELOG.md]: CHANGELOG.md