Java 使用 json.simple 将字符串转换为 json 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30757136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 10:07:30  来源:igfitidea点击:

Converting string to json object using json.simple

javajson

提问by Talib

I am using org.json.simple.JSONObject. I want to convert stringto Json object.

我正在使用org.json.simple.JSONObject. 我想转换string为 Json object

String value=request.getParameter("savepos");
JSONObject jsonObject = (JSONObject) JSONValue.parse(value);

It doesn't work. Why?

它不起作用。为什么?

采纳答案by Shailesh Yadav

Try this:

尝试这个:

JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(stringToParse);

回答by Arthur Eirich

JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(value);

should do the work.

应该做的工作。

回答by Anzar Ansari

Converting String to Json Object by using org.json.simple.JSONObject

使用 org.json.simple.JSONObject 将字符串转换为 Json 对象

private static JSONObject createJSONObject(String jsonString){
    JSONObject  jsonObject=new JSONObject();
    JSONParser jsonParser=new  JSONParser();
    if ((jsonString != null) && !(jsonString.isEmpty())) {
        try {
            jsonObject=(JSONObject) jsonParser.parse(jsonString);
        } catch (org.json.simple.parser.ParseException e) {
            e.printStackTrace();
        }
    }
    return jsonObject;
}