net.sf.json.JSONObject string to json
时间: 2023-09-27 09:07:47 浏览: 149
使用JSONObject解析json数据
To convert a JSON string to a JSON object using `net.sf.json.JSONObject`, you can follow these steps:
1. Import the required classes:
```java
import net.sf.json.JSONObject;
```
2. Create a `JSONObject` instance and pass the JSON string as a parameter to its constructor:
```java
String jsonString = "{\"name\": \"John\", \"age\": 30}";
JSONObject jsonObject = new JSONObject(jsonString);
```
Now, the `jsonObject` variable holds the JSON object representation of the provided JSON string.
You can access the values in the JSON object using various methods provided by the `JSONObject` class. For example:
```java
String name = jsonObject.getString("name");
int age = jsonObject.getInt("age");
```
Make sure you have the `json-lib` library in your classpath to use `net.sf.json.JSONObject`.
阅读全文