我有一个名为jsonString的字符串变量:

{"phonetype":"N95","cat":"WP"}

现在我想把它转换成JSON对象。我在谷歌上搜索了更多,但没有得到任何预期的答案!


当前回答

最好使用更简单的方式使用org。json自由。只需要做一个非常简单的方法:

JSONObject obj = new JSONObject();
obj.put("phonetype", "N95");
obj.put("cat", "WP");

现在obj是你各自String的JSONObject转换形式。这是在有名称-值对的情况下。

对于字符串,可以直接传递给JSONObject的构造函数。如果它是一个有效的json String,那么好,否则它会抛出异常。

其他回答

要将String转换为JSONObject,您只需要将String实例传递到JSONObject的构造函数中。

Eg:

JSONObject jsonObj = new JSONObject("your string");

字符串到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, sting就像json一样。{“phonetype”:“N95”,“猫”:“WP”}

String Data=response.getEntity().getText().toString(); // reading the string value 
JSONObject json = (JSONObject) new JSONParser().parse(Data);
String x=(String) json.get("phonetype");
System.out.println("Check Data"+x);
String y=(String) json.get("cat");
System.out.println("Check Data"+y);

从JSON主页可以链接到各种Java JSON序列化器和反序列化器。

在撰写本文时,有22种:

JSON-java. JSONUtil. JSONP. 杰森-利布。 字符串树。 索乔。 json-taglib. Flexjson。 阿尔戈。 杰索尼。 法斯特森。 姆森。 杰森。 JSON-简单。 json-io. 谷歌-格森。 福斯新星JSON。 玉米转换器。 阿帕奇·约翰松。 根森。 库克森。 程序库。

...当然,这个名单是可以改变的。

不需要使用任何外部库。

你可以使用这个类代替:)(处理甚至列表,嵌套列表和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(