{
"data":
{
"map":
{
"allowNestedValues": true,
"create": "2012-12-11 15:16:13",
"title": "test201212110004",
"transitions": []
}
},
"msg": "success",
"code": "0"
}
Arriba hay un JsonObject, el dataes un JsonObject.
Cómo convertirlo en un me Stringgusta "msg":"success"como sabe, no puedo agregar directamente comillas dobles fuera datadel valor.

\"si eso es lo que desea. Por favor, agregue su problema / pregunta.org.json.JSONObject? Si es así, simplemente puede llamar altoString()método deJSONObjectpara obtener el texto JSON deJSONObject.Respuestas:
Hay un método incorporado para convertir un JSONObject en un String. ¿Por qué no usas eso?
JSONObject json = new JSONObject(); json.toString();fuente
Puedes usar:
JSONObject jsonObject = new JSONObject(); jsonObject.toString();Y si desea obtener un valor específico, puede usar:
jsonObject.getString("msg");o valor entero
jsonObject.getInt("codeNum");fuente
puedes usar
JsonObject.getString("msg");fuente
Puede probar el convertidor Gson, para obtener la conversión exacta como json.stringify
fuente
Agregue comillas dobles fuera de los corchetes y reemplace las comillas dobles dentro de
{}con\"Entonces:
"{\"data\":{..... }"fuente
JSONObject metadata = (JSONObject) data.get("map"); //for example String jsonString = metadata.**toJSONString()**;fuente
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"));fuente
Puede utilizar la biblioteca confiable 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());fuente
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"}Use JSON.stringify para convertir datos completos en una cadena como se muestra a continuación
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"}"Use JSON.parse para convertir el objeto de cadena completo en un objeto JSON como se muestra a continuación
var orgdata = JSON.parse(stringData);o / p:
Object {data: Object, msg: "success", code: "0"}fuente
Creo que necesitas esto:
{"ParamOne":"InnerParamOne":"InnerParamOneValue","InnerParamTwo":"InnerParamTwoValue","InnerParamThree":"InnerParamThreeValue","InnerParamFour":"InnerParamFourValue","InnerParamFive":"InnerParamFiveValue"}}String response = {\"ParamOne\":{\"InnerParamOne\":\"InnerParamOneValue\",\"InnerParamTwo\":\"InnerParamTwoValue\",\"InnerParamThree\":\"InnerParamThreeValue\",\"InnerParamFour\":\"InnerParamFourValue\",\"InnerParamFive\":\"InnerParamFiveValue\"}} ;Simplemente reemplace "por \"
fuente