我想从JSON文件中删除flavor和pkgT字段,每行格式如下:
"jobID":2598752", "account":"TG-CCR120014", "user":"charngda", "pkgT":{"mvapich2-new/1.2":{"libA":["libmpich.so.1.1"], "flavor":["default:pgi/7.2-5"]}}, "startEpoch":"1338608868", "runTime":"48", "execType":"user:binary", "exec":"IOR", "numNodes":"4", "sha1":"755187bd8550881bb0c9951822e74a9a53c8d0f3", "execEpoch":1336757832, "execModify":"Fr, Ma, 1, 12:37:1, 2012", "startTime":"Fr, Ju, , 22:47:4, 2012", "numCores":"64", "sizeT":{"bss":"36224", "text":"3502656", "data":"128944"}},
那么如何构造正则表达式来删除这些字段呢?谢谢!!!!
好的,我在JobID字段中修复了缺失的引用,现在是什么?
答案 0 :(得分:0)
由于您的字符串已经是JSON,为什么要使用正则表达式?这只是使问题过于复杂。相反,请使用您的字符串并使用带有JSON.parse()
的JSON解析它。
var str = '{ "jobID": "2598752", "account": "TG-CCR120014", "user": "charngda", "pkgT": { "mvapich2-new/1.2": { "libA": ["libmpich.so.1.1"], "flavor": ["default:pgi/7.2-5"] } }, "startEpoch": "1338608868", "runTime": "48", "execType": "user:binary", "exec": "IOR", "numNodes": "4", "sha1": "755187bd8550881bb0c9951822e74a9a53c8d0f3", "execEpoch": 1336757832, "execModify": "Fr, Ma, 1, 12:37:1, 2012", "startTime": "Fr, Ju, , 22:47:4, 2012", "numCores": "64", "sizeT": { "bss": "36224", "text": "3502656", "data": "128944" } }';
var obj = JSON.parse(str);
然后继续删除密钥。
delete obj['flavor'];
delete obj['pkgT'];
//returns true if deleted, false if failed
然后你可以将JSON变回一个字符串。
var newstr = JSON.stringify(obj);