[英]How to create a Java JSON Object from a Json string?
I'm trying to create a Json Object in java from a Json String but i'm not using it in the proper way since the json object looks empty 我正在尝试从Json字符串在Java中创建一个Json对象,但由于json对象看起来是空的,所以我没有以正确的方式使用它
Here is my Json arriving from Ajax and it is composed by an array and a single variable: 这是我从Ajax到达的Json,它由一个数组和一个变量组成:
{"arrayUsers":["53","54"],"idMansione":"1"}
Here is my Java function where i'm trying to create the Json Object: 这是我尝试创建Json对象的Java函数:
import org.json.JSONArray;
import org.json.JSONObject;
...
public ResponseEntity<String> assignTask(String json) {
//Here i'm trying to create the object
JSONObject mainObj = new JSONObject();
mainObj.put("arrayUsers", json);
System.out.print(mainObj); //looks empty
return new ResponseEntity<String>("OK", HttpStatus.OK);
}
Try this: 尝试这个:
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(stringToParse);
只是:
JSONObject mainObj = new JSONObject(json);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.