帮我把这段代码改成接收String类型的数据并可以解析
时间: 2023-05-24 07:02:41 浏览: 46
Sure, here's your modified code that accepts a String input and can parse it:
```java
import org.json.JSONObject;
import org.json.JSONException;
public static void parseString(String input) {
try {
JSONObject obj = new JSONObject(input);
String status = obj.getString("status");
int code = obj.getInt("code");
String message = obj.getString("message");
System.out.println("Status: " + status);
System.out.println("Code: " + code);
System.out.println("Message: " + message);
} catch (JSONException e) {
e.printStackTrace();
}
}
```
You can call this method with a String input:
```java
String input = "{\"status\":\"success\",\"code\":200,\"message\":\"Data fetched successfully\"}";
parseString(input);
```
This will output:
```
Status: success
Code: 200
Message: Data fetched successfully
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)