我有一个名为jsonString的字符串变量:
{"phonetype":"N95","cat":"WP"}
现在我想把它转换成JSON对象。我在谷歌上搜索了更多,但没有得到任何预期的答案!
我有一个名为jsonString的字符串变量:
{"phonetype":"N95","cat":"WP"}
现在我想把它转换成JSON对象。我在谷歌上搜索了更多,但没有得到任何预期的答案!
当前回答
我喜欢为此使用google-gson,这正是因为我不需要直接使用JSONObject。
在这种情况下,我将有一个类,将对应于JSON对象的属性
class Phone {
public String phonetype;
public String cat;
}
...
String jsonString = "{\"phonetype\":\"N95\",\"cat\":\"WP\"}";
Gson gson = new Gson();
Phone fooFromJson = gson.fromJson(jsonString, Phone.class);
...
然而,我认为你的问题更像是,我如何从JSON字符串中得到一个实际的JSONObject对象。
我在看google-json api,找不到任何直接的东西 org。json的api,这可能是你想要使用的,如果你非常需要使用一个基本的JSONObject。
http://www.json.org/javadoc/org/json/JSONObject.html
使用org.json.JSONObject(另一个完全不同的API)如果你想做一些像…
JSONObject jsonObject = new JSONObject("{\"phonetype\":\"N95\",\"cat\":\"WP\"}");
System.out.println(jsonObject.getString("phonetype"));
我认为google-gson的美妙之处在于您不需要处理JSONObject。你只需要获取json,传递想要反序列化的类,你的类属性就会与json匹配,但话说回来,每个人都有自己的需求,也许你不能奢侈地在反序列化端拥有预先映射的类,因为json生成端可能太动态了。在这种情况下,只需使用json.org。
其他回答
对于仍在寻找答案的人:
JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(stringToParse);
那些因为弃用问题而无法从发布的答案中找到解决方案的人,可以使用com.google.gson中的JsonParser。
例子:
JsonObject jsonObject = JsonParser.parseString(jsonString).getAsJsonObject();
System.out.println(jsonObject.get("phonetype"));
System.out.println(jsonObject.get("cat"));
字符串到JSON使用Jackson com.fasterxml.jackson.databind:
假设你的json-string表示如下:jsonString = {"phonetype":"N95","cat":"WP"}
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Simple code exmpl
*/
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(jsonString);
String phoneType = node.get("phonetype").asText();
String cat = node.get("cat").asText();
不需要使用任何外部库。
你可以使用这个类代替:)(处理甚至列表,嵌套列表和json)
public class Utility {
public static Map<String, Object> jsonToMap(Object json) throws JSONException {
if(json instanceof JSONObject)
return _jsonToMap_((JSONObject)json) ;
else if (json instanceof String)
{
JSONObject jsonObject = new JSONObject((String)json) ;
return _jsonToMap_(jsonObject) ;
}
return null ;
}
private static Map<String, Object> _jsonToMap_(JSONObject json) throws JSONException {
Map<String, Object> retMap = new HashMap<String, Object>();
if(json != JSONObject.NULL) {
retMap = toMap(json);
}
return retMap;
}
private static Map<String, Object> toMap(JSONObject object) throws JSONException {
Map<String, Object> map = new HashMap<String, Object>();
Iterator<String> keysItr = object.keys();
while(keysItr.hasNext()) {
String key = keysItr.next();
Object value = object.get(key);
if(value instanceof JSONArray) {
value = toList((JSONArray) value);
}
else if(value instanceof JSONObject) {
value = toMap((JSONObject) value);
}
map.put(key, value);
}
return map;
}
public static List<Object> toList(JSONArray array) throws JSONException {
List<Object> list = new ArrayList<Object>();
for(int i = 0; i < array.length(); i++) {
Object value = array.get(i);
if(value instanceof JSONArray) {
value = toList((JSONArray) value);
}
else if(value instanceof JSONObject) {
value = toMap((JSONObject) value);
}
list.add(value);
}
return list;
}
}
将JSON字符串转换为hashmap使用:
HashMap<String, Object> hashMap = new HashMap<>(Utility.jsonToMap(
使用org.json.simple.JSONObject将String转换为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;
}