Home >Java >javaTutorial >How to Convert JSON Strings to JSONObjects in Java?

How to Convert JSON Strings to JSONObjects in Java?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-04 09:12:35708browse

How to Convert JSON Strings to JSONObjects in Java?

Converting JSON strings to JSONObjects in Java

Suppose you have a string variable containing a JSON string:

jsonString: "{ "phonetype": "N95", "cat": "WP" }"

You can convert this JSON string to a JSONObject using libraries like org.json:

try {
    JSONObject jsonObject = new JSONObject(jsonString);
} catch (JSONException err) {
    System.out.println("Error: " + err.toString());
}

The JSONObject can now be accessed and modified as needed.

The above is the detailed content of How to Convert JSON Strings to JSONObjects 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