96
{
    "data": 
    {
        "map":
        {
            "allowNestedValues": true,
            "create": "2012-12-11 15:16:13",
            "title": "test201212110004",
            "transitions": []
        }
    },
    "msg": "success",
    "code": "0"
}

上面是a JsonObjectdata是a JsonObject

如您所知,如何将其转换为Stringlike "msg":"success",我不能直接在data's 值之外添加双引号。

4

13 回答 13

188

有一个内置方法可以将 JSONObject 转换为 String。你为什么不使用它:

JSONObject json = new JSONObject();

json.toString();
于 2013-07-15T10:01:57.143 回答
22

You can use:

JSONObject jsonObject = new JSONObject();
jsonObject.toString();

And if you want to get a specific value, you can use:

jsonObject.getString("msg");

or Integer value

jsonObject.getInt("codeNum");
于 2018-04-18T01:28:43.730 回答
8

您可以使用

JsonObject.getString("msg"); 
于 2013-07-15T10:01:40.070 回答
4

您可以尝试 Gson 转换器,以获得像 json.stringify 这样的精确转换

val jsonString:String = jsonObject.toString()
val gson:Gson = GsonBuilder().setPrettyPrinting().create()
val json:JsonElement = gson.fromJson(jsonString,JsonElement.class)
val jsonInString:String= gson.toJson(json)
println(jsonInString)
于 2019-05-18T12:57:49.457 回答
2

JsonObject 似乎是 JSON-P API。如果这是真的,我会使用 JsonWritter 将 JsonValue 写入 StringWriter:

    JsonObjectBuilder pokemonBuilder = Json.createObjectBuilder();
    pokemonBuilder.add("name", "Pikachu");
    pokemonBuilder.add("type", "electric");
    pokemonBuilder.add("cp", 827);
    pokemonBuilder.add("evolve", true);
    JsonObject pokemon = pokemonBuilder.build();
    StringWriter sw = new StringWriter(128);
    try (JsonWriter jw = Json.createWriter(sw)) {
        jw.write(pokemon);
    }
    String pokemonStr = sw.toString();
于 2021-05-20T10:45:13.707 回答
1

在括号外添加双引号,并用{}with替换内的双引号\"

所以:"{\"data\":{..... }"

于 2016-02-16T13:48:26.453 回答
0

用这个 :

JSONObject json = new JSONObject();
JSONObject.valueToString(json.toString());
于 2022-02-20T08:52:31.153 回答
0
JSONObject metadata = (JSONObject) data.get("map"); //for example
String jsonString = metadata.**toJSONString()**;
于 2017-04-13T10:57:50.903 回答
0

只需使用 ObjectMapper

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,false);
//here more config opts...
Car car = new Car("yellow", "renault");
objectMapper.writeValue(new File("target/car.json"), car);
String carAsString = objectMapper.writeValueAsString(car);
于 2021-12-08T11:46:17.533 回答
-2

您可以使用可靠的库 GSON

private static final Type DATA_TYPE_JSON = 
        new TypeToken<JSONObject>() {}.getType();           
JSONObject orderJSON = new JSONObject();
orderJSON.put("noOfLayers", "2");
orderJSON.put("baseMaterial", "mat");
System.out.println("JSON == "+orderJSON.toString());
String dataAsJson = new Gson().toJson(orderJSON, DATA_TYPE_JSON);
System.out.println("Value of dataAsJson == "+dataAsJson.toString());
String data = new Gson().toJson(dataAsJson);
System.out.println("Value of jsonString == "+data.toString());
于 2017-12-15T12:33:01.937 回答
-2
     This should get all the values from the above JsonObject  
     System.out.println(jsonObj.get("msg"));
     System.out.println(jsonObj.get("code"));

     JsonObject obj= jsonObj.get("data").getAsJsonObject().get("map").getAsJsonObject();
     System.out.println(obj.get("allowNestedValues"));
     System.out.println(obj.get("create"));
     System.out.println(obj.get("title"));
     System.out.println(obj.get("transitions"));
于 2016-12-06T15:02:25.020 回答
-4
 var data= {"data": {"map":{"allowNestedValues": true,"create": "2012-12-11 15:16:13","title": "test201212110004","transitions": []}},"msg": "success","code": "0"}

o/p:

Object {data: Object, msg: "success", code: "0"}

使用 JSON.stringify 将整个数据转换为字符串,如下所示

var stringData = JSON.stringify(data);

o/p:

"{"data":{"map":{"allowNestedValues":true,"create":"2012-12-11 15:16:13","title":"test201212110004","transitions":[]}},"msg":"success","code":"0"}"

使用 JSON.parse 将整个字符串对象转换为 JSON 对象,如下所示

var orgdata = JSON.parse(stringData);

o/p:

Object {data: Object, msg: "success", code: "0"}
于 2017-04-17T14:22:27.727 回答
-7

我认为你需要这个:

假设您有这样的示例JSON

{"ParamOne":"InnerParamOne":"InnerParamOneValue","InnerParamTwo":"InnerParamTwoValue","InnerParamThree":"InnerParamThreeValue","InnerParamFour":"InnerParamFourValue","InnerParamFive":"InnerParamFiveValue"}}

转换为字符串:

String response = {\"ParamOne\":{\"InnerParamOne\":\"InnerParamOneValue\",\"InnerParamTwo\":\"InnerParamTwoValue\",\"InnerParamThree\":\"InnerParamThreeValue\",\"InnerParamFour\":\"InnerParamFourValue\",\"InnerParamFive\":\"InnerParamFiveValue\"}} ;

只需将“替换为\”

于 2015-08-21T05:39:32.920 回答