JSON 또는 객체가 서식(들여쓰기 + 줄 바꿈)Markdown의 json 코드 블록 문자열로 변환됨

29244 단어
소월석은 업무 중에 백엔드에서 온 대상을md 형식으로 바꾸어 과장해야 하는 필요성을 만났다.먼저 대상을 json으로 직접 전환한 다음에 앞뒤로 각각 ```json```을 연결하면 된다.그러나 효과는 다음과 같다
{"err_status":0,"principal":null,"_id":"5eaac83927e3e77ecc7e6a68","ip":"::ffff:127.0.0.1","type":"POST","err_type":"TypeError","err_msg":"TypeError: articles.aggregate(...).populate is not a function
\t at module.exports (C:\\Users\\w\\Desktop\\blog\\blog-api\\router\\routers\\article\\getAllArticle.js:23:12)
\t at C:\\Users\\w\\Desktop\\blog\\blog-api\\router\\routers\\article\\index.js:30:34
\t at Layer.handle [as handle_request] (C:\\Users\\w\\Desktop\\blog\\blog-api\
ode_modules\\express\\lib\\router\\layer.js:95:5)
\t at next (C:\\Users\\w\\Desktop\\blog\\blog-api\
ode_modules\\express\\lib\\router\\route.js:137:13)
\t at Route.dispatch (C:\\Users\\w\\Desktop\\blog\\blog-api\
ode_modules\\express\\lib\\router\\route.js:112:3)
\t at Layer.handle [as handle_request] (C:\\Users\\w\\Desktop\\blog\\blog-api\
ode_modules\\express\\lib\\router\\layer.js:95:5)
\t at C:\\Users\\w\\Desktop\\blog\\blog-api\
ode_modules\\express\\lib\\router\\index.js:281:22
\t at Function.process_params (C:\\Users\\w\\Desktop\\blog\\blog-api\
ode_modules\\express\\lib\\router\\index.js:335:12)
\t at next (C:\\Users\\w\\Desktop\\blog\\blog-api\
ode_modules\\express\\lib\\router\\index.js:275:10)
\t at Function.handle (C:\\Users\\w\\Desktop\\blog\\blog-api\
ode_modules\\express\\lib\\router\\index.js:174:3)
\t at router (C:\\Users\\w\\Desktop\\blog\\blog-api\
ode_modules\\express\\lib\\router\\index.js:47:12)
\t at Layer.handle [as handle_request] (C:\\Users\\w\\Desktop\\blog\\blog-api\
ode_modules\\express\\lib\\router\\layer.js:95:5)
\t at trim_prefix (C:\\Users\\w\\Desktop\\blog\\blog-api\
ode_modules\\express\\lib\\router\\index.js:317:13)
\t at C:\\Users\\w\\Desktop\\blog\\blog-api\
ode_modules\\express\\lib\\router\\index.js:284:7
\t at Function.process_params (C:\\Users\\w\\Desktop\\blog\\blog-api\
ode_modules\\express\\lib\\router\\index.js:335:12)
\t at Immediate.next (C:\\Users\\w\\Desktop\\blog\\blog-api\
ode_modules\\express\\lib\\router\\index.js:275:10)"
,"req_data":{"body":{"className":" "},"query":{}},"err_file":"C:\\Users\\w\\Desktop\\blog\\blog-api\\router\\routers\\article\\getAllArticle.js","date":"2020-04-30 20:44:41","__v":0}

렌더링 결과는 json 줄넘기와 포맷을 하지 않습니다. (CSDN조차도 자동으로 포맷할 수 없습니다. 이것은 스스로 실현해야 할 것으로 예상됩니다.)그리고 나는 쉼표 뒤에 줄을 바꾸려고 시도했다. (효과는 다음과 같다.)
{"err_status":0,
"principal":null,
"_id":"5eaac83927e3e77ecc7e6a68",
"ip":"::ffff:127.0.0.1",
"type":"POST",
"err_type":"TypeError",
"err_msg":"TypeError: articles.aggregate(...).populate is not a function
\t at module.exports (C:\\Users\\w\\Desktop\\blog\\blog-api\\router\\routers\\article\\getAllArticle.js:23:12)
\t at C:\\Users\\w\\Desktop\\blog\\blog-api\\router\\routers\\article\\index.js:30:34
\t at Layer.handle [as handle_request] (C:\\Users\\w\\Desktop\\blog\\blog-api\
ode_modules\\express\\lib\\router\\layer.js:95:5)
\t at next (C:\\Users\\w\\Desktop\\blog\\blog-api\
ode_modules\\express\\lib\\router\\route.js:137:13)
\t at Route.dispatch (C:\\Users\\w\\Desktop\\blog\\blog-api\
ode_modules\\express\\lib\\router\\route.js:112:3)
\t at Layer.handle [as handle_request] (C:\\Users\\w\\Desktop\\blog\\blog-api\
ode_modules\\express\\lib\\router\\layer.js:95:5)
\t at C:\\Users\\w\\Desktop\\blog\\blog-api\
ode_modules\\express\\lib\\router\\index.js:281:22
\t at Function.process_params (C:\\Users\\w\\Desktop\\blog\\blog-api\
ode_modules\\express\\lib\\router\\index.js:335:12)
\t at next (C:\\Users\\w\\Desktop\\blog\\blog-api\
ode_modules\\express\\lib\\router\\index.js:275:10)
\t at Function.handle (C:\\Users\\w\\Desktop\\blog\\blog-api\
ode_modules\\express\\lib\\router\\index.js:174:3)
\t at router (C:\\Users\\w\\Desktop\\blog\\blog-api\
ode_modules\\express\\lib\\router\\index.js:47:12)
\t at Layer.handle [as handle_request] (C:\\Users\\w\\Desktop\\blog\\blog-api\
ode_modules\\express\\lib\\router\\layer.js:95:5)
\t at trim_prefix (C:\\Users\\w\\Desktop\\blog\\blog-api\
ode_modules\\express\\lib\\router\\index.js:317:13)
\t at C:\\Users\\w\\Desktop\\blog\\blog-api\
ode_modules\\express\\lib\\router\\index.js:284:7
\t at Function.process_params (C:\\Users\\w\\Desktop\\blog\\blog-api\
ode_modules\\express\\lib\\router\\index.js:335:12)
\t at Immediate.next (C:\\Users\\w\\Desktop\\blog\\blog-api\
ode_modules\\express\\lib\\router\\index.js:275:10)"
, "req_data":{"body":{"className":" "}, "query":{}}, "err_file":"C:\\Users\\w\\Desktop\\blog\\blog-api\\router\\routers\\article\\getAllArticle.js", "date":"2020-04-30 20:44:41", "__v":0}

뒤에 여러 가지 리플렉스 규칙을 시도했는데 효과가 안 좋았어요.
어쩔 수 없이 문자열을 생성하는 규칙을 스스로 봉인할 수밖에 없다(당시 사용한 것은 vue 개발이었기 때문에 방법에 대해 i10-를 호환할 계획은 없었다). 코드는 다음과 같다.
function ObjToRetractJsonStr(obj, num) {
              if(!num){//     
                num = 1;
              }
              let t = ""
              for(let k in obj){
                if(obj[k] !== null && typeof obj[k] === "object"){
                  t += `${"\t".repeat(num)}"${k}": ${ObjToRetractJsonStr(obj[k],num+1)},
`
}else { t += `${"\t".repeat(num)}"${k}": ${typeof obj[k] === "string"?"\""+obj[k]+"\"":obj[k]},
`
} } //} ] if(Array.isArray(obj)){ // [] return `[
${t}${"\t".repeat(num-1)}]`
} return `{
${t}${"\t".repeat(num-1)}}`
} // md json text = "```json
"
+ObjToRetractJsonStr(this.resData)+"
```"

효과는 다음과 같다.
{
	"err_status": 0,
	"principal": null,
	"_id": "5eaac83927e3e77ecc7e6a68",
	"ip": "::ffff:127.0.0.1",
	"type": "POST",
	"err_type": "TypeError",
	"err_msg": "TypeError: articles.aggregate(...).populate is not a function
	   at module.exports (C:\Users\w\Desktop\blog\blog-api\router\routers\article\getAllArticle.js:23:12)
	   at C:\Users\w\Desktop\blog\blog-api\router\routers\article\index.js:30:34
	   at Layer.handle [as handle_request] (C:\Users\w\Desktop\blog\blog-api
ode_modules\express\lib\router\layer.js:95:5) at next (C:\Users\w\Desktop\blog\blog-api
ode_modules\express\lib\router\route.js:137:13) at Route.dispatch (C:\Users\w\Desktop\blog\blog-api
ode_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (C:\Users\w\Desktop\blog\blog-api
ode_modules\express\lib\router\layer.js:95:5) at C:\Users\w\Desktop\blog\blog-api
ode_modules\express\lib\router\index.js:281:22 at Function.process_params (C:\Users\w\Desktop\blog\blog-api
ode_modules\express\lib\router\index.js:335:12) at next (C:\Users\w\Desktop\blog\blog-api
ode_modules\express\lib\router\index.js:275:10) at Function.handle (C:\Users\w\Desktop\blog\blog-api
ode_modules\express\lib\router\index.js:174:3) at router (C:\Users\w\Desktop\blog\blog-api
ode_modules\express\lib\router\index.js:47:12) at Layer.handle [as handle_request] (C:\Users\w\Desktop\blog\blog-api
ode_modules\express\lib\router\layer.js:95:5) at trim_prefix (C:\Users\w\Desktop\blog\blog-api
ode_modules\express\lib\router\index.js:317:13) at C:\Users\w\Desktop\blog\blog-api
ode_modules\express\lib\router\index.js:284:7 at Function.process_params (C:\Users\w\Desktop\blog\blog-api
ode_modules\express\lib\router\index.js:335:12) at Immediate.next (C:\Users\w\Desktop\blog\blog-api
ode_modules\express\lib\router\index.js:275:10)", "req_data": { "body": { "className": " ", }, "query": { }, }, "err_file": "C:\Users\w\Desktop\blog\blog-api\router\routers\article\getAllArticle.js", "date": "2020-04-30 20:44:41", "__v": 0, }

요구에 도달(내용 점프는 json이 밝지 않습니다. 점프를 하려면 점프를 하지 않으면 됩니다)
IE 호환 방안 추천(하늘을 나는 이론일 뿐이다. 작은 달밤 당시의 시간이 넉넉하지 않아 구체적인 코드를 밝힐 수 없다) 창고 계산 산식의 원리를 참고하여 포맷되지 않은 json 전송에 대해 관건적인 부합을 분리한 후에 창고 조작을 시도할 수 있다.현재 데이터의 깊이를 기록하고 그 안에 대응하는 수량의 축진과 줄 바꾸기를 넣으면 된다.

좋은 웹페이지 즐겨찾기