Home > Java > javaTutorial > How to Convert a JSON String to a JSONObject in Java?

How to Convert a JSON String to a JSONObject in Java?

Susan Sarandon
Release: 2024-12-27 04:50:11
Original
238 people have browsed it

How to Convert a JSON String to a JSONObject in Java?

Converting JSON String to JSONObject in Java

If you encounter difficulties in converting a JSON string to a JSONObject, consider the following straightforward solution. Suppose you have a JSON string stored in a String variable called jsonString:

{
  "phonetype": "N95",
  "cat": "WP"
}
Copy after login

Using the org.json Library:

To access the functionality of the org.json library, follow these steps:

  1. In your project, include the "org.json" library as a dependency.
  2. Create a JSONObject object using the following syntax:
try {
    JSONObject jsonObject = new JSONObject("{\"phonetype\":\"N95\",\"cat\":\"WP\"}");
}catch (JSONException err){
    Log.d("Error", err.toString());
}
Copy after login

By using this approach, you can seamlessly convert a JSON string into a valid JSONObject object.

The above is the detailed content of How to Convert a JSON String to a JSONObject in Java?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template