Development Tip

JsonObject를 문자열로 변환

yourdevel 2020. 11. 30. 20:04
반응형

JsonObject를 문자열로 변환


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

위는 JsonObject, dataJsonObject.

아시다시피 String좋아 하는 것으로 변환하는 방법은 의 값 "msg":"success"외부에 큰 따옴표를 직접 추가 할 수 없습니다 data.


@hsz 우리는 JsonObject를 String으로 변환하는 내장 메소드가 있습니다. 그걸 사용하지 그래.

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

당신이 사용할 수있는

JsonObject.getString("msg"); 

당신이 사용할 수있는:

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

특정 값을 얻으려면 다음을 사용할 수 있습니다.

jsonObject.getString("msg");

또는 정수 값

jsonObject.getInt("codeNum");

괄호 밖에서 따옴표를 추가하고 내부 따옴표 교체 {}로를\"

그래서: "{\"data\":{..... }"


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)

JSONObject metadata = (JSONObject) data.get("map"); //for example
String jsonString = metadata.**toJSONString()**;

     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"));

신뢰할 수있는 라이브러리 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());

 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"}

나는 이것이 필요하다고 생각한다.

Suppose you have Sample JSON like this :

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

Converted to String :

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

Just replace " by \"

참고URL : https://stackoverflow.com/questions/17651395/convert-jsonobject-to-string

반응형