java 字符串到json的转换问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12727742/
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
String to json conversion issue
提问by pars
I am trying to convert a string to JSONObject
object using the below code,but i am getting
我正在尝试JSONObject
使用以下代码将字符串转换为对象,但我得到了
Exception in thread "main" java.lang.ClassCastException:
org.json.simple.JSONObject cannot be cast to net.sf.json.JSONObject .
Source:
来源:
import net.sf.json.JSONObject;
import org.json.simple.parser.JSONParser;
public static void run(JSONObject jsonObject) {
System.out.println("in run--");
}
public static void main(String[] args) throws Exception {
System.out.println("here");
String json = "{\"task\": \"com.ge.dbt.workers.surveytoexcel.worker.SurveyWorker\",\"prod_id\": 12345,\"survey_id\": 5666,\"person_id\": 18576567,\"req_date\": \"1212\"}";
JSONObject jsonObj;
JSONParser parser = new JSONParser();
Object obj = parser.parse(json);
jsonObj = (JSONObject) obj;
run(jsonObj);
}
What is wrong here?
这里有什么问题?
采纳答案by David Grant
You've imported JSONObject
from the wrong package. Change this line:
您JSONObject
从错误的包中导入。改变这一行:
import net.sf.json.JSONObject;
to this:
对此:
import org.json.simple.JSONObject;
回答by Nokia808Freak
Implementing the following solution, You don't even have to bother about a parser...
实施以下解决方案,您甚至不必担心解析器......
The Problem here is that u're trying to cast a object of type org.json.simple.JSONObject
to net.sf.json.JSONObject
. You might wanna try The package org.codehaus.jettison.json.JSONObject
. that is enough to do all the required things.
这里的问题是你试图将一个类型的对象强制转换org.json.simple.JSONObject
为net.sf.json.JSONObject
. 你可能想试试 The package org.codehaus.jettison.json.JSONObject
。这足以做所有需要的事情。
Simple Example:
简单示例:
First, Prepare a String:
首先,准备一个字符串:
String jStr = "{\"name\":\"Fred\",\"Age\":27}";
String jStr = "{\"name\":\"Fred\",\"Age\":27}";
Now, to parse the String
Object, U just have to pass the String to the JSONObject();
constructor method
现在,要解析String
对象,您只需将字符串传递给JSONObject();
构造函数方法
JSONObject jObj = new JSONObject(jStr);
JSONObject jObj = new JSONObject(jStr);
That should do it and voila! You have a JSONObject. Now you can play with it as u please.
那应该这样做,瞧!你有一个 JSONObject。现在你可以随意玩了。
How So Simple ain't it?
多么简单,不是吗?
The Modified version of the Code might look like:
代码的修改版本可能如下所示:
import net.sf.json.JSONObject;
import net.sf.json.JSONObject;
import org.codehaus.jettison.json.JSONObject;
public static void run(JSONObject jsonObject) {
System.out.println("in run-- "+jsonObject.getInt("person_id"));
}
public static void main(String[] args) throws Exception {
System.out.println("here");
String json = "{\"task\": \"com.ge.dbt.workers.surveytoexcel.worker.SurveyWorker\",\"prod_id\": 12345,\"survey_id\": 5666,\"person_id\": 18576567,\"req_date\": \"1212\"}";
JSONObject jsonObj = new JSONObject(json);
run(jsonObj);
}
with JSON, It's SssOooooooo simple
使用 JSON,SssOoooooooo 很简单