2017-03-15 25 views
1
{"empid":{"string":"31564604"},"joindate":{"date":2017-01-01}} 

使用Java/Javscript将上面的json转换为下面的格式。 juzt需要删除数据类型。Json - 只删除密钥并赋值给父键

{"empid":"31564604","joindate":2017-01-01} 
+0

不是一个有效的json,'2017-01-01' –

+3

也许选择你正在使用的语言,并显示你已经尝试过。 – AntonH

+0

你用什么库解析创建jsons? – Luci

回答

0

使用JavaScript,将其转换为一个对象,然后通过简单地在所有属性迭代更新,最后字符串化的对象。

var json = '{"empid":{"string":"31564604"},"joindate":{"date":"2017-01-01"}}'; 
 

 
// parse the string 
 
var obj = JSON.parse(json); 
 
// get all keys and iterate over them 
 
Object.keys(obj).forEach(function(k) { 
 
    // update the property with the nested object property value 
 
    obj[k] = obj[k][Object.keys(obj[k])[0]]; 
 
}) 
 

 
// convert back to JSON 
 
console.log(JSON.stringify(obj));


UPDATE:对于嵌套对象使用与递归方法是相同的。

var json = '{"empid":{"string":"31564604"},"joindate":{"date":"2017-01-01"},"a":{"array":[{"number":1}]},"level1":{"object":{"level2":{"string":"abc"}}}}'; 
 

 
// parse the string 
 
var object = JSON.parse(json); 
 
updateObj(object); 
 

 
function updateObj(obj) { 
 
    // get all keys and iterate over them 
 
    Object.keys(obj).forEach(function(k) { 
 
    // update the property with the nested object property value 
 
    obj[k] = obj[k][Object.keys(obj[k])[0]]; 
 
    // recursively call the faction if the element is object 
 
    typeof obj[k] == 'object' && updateObj(obj[k]); 
 
    }) 
 
} 
 

 
// convert back to JSON 
 
console.log(JSON.stringify(object));

+0

感谢Pranav ...让我尝试实际的JSON,它有很多嵌套循环... –

+0

@karthickRangarajan:为递归方法更新 –

0

另一种解决方案,使用Array#reduce

var obj = { empid: { string: 31564604 }, joindate: { date: '2017-01-01' }, nested: { nextLevel: { boolean: 1 } } }, 
 

 
    newObj = Object.keys(obj).reduce(function(s, a) { 
 
     s[a] = obj[a][Object.keys(obj[a])] 
 
     return s; 
 
    }, {}); 
 

 
    console.log(newObj);

0

你可以使用一个递归方法与对象用于获取类型的权利。

function convert(object) { 
 
    var dataTypes = { boolean: true, string: true, date: true }; 
 

 
    Object.keys(object).forEach(function (k) { 
 
     var key; 
 
     if (object[k] && typeof object[k] === 'object') { 
 
      key = Object.keys(object[k])[0]; 
 
      if (key in dataTypes) { 
 
       object[k] = object[k][key]; 
 
      } else { 
 
       convert(object[k]); 
 
      } 
 
     } 
 
    }); 
 
} 
 

 
var object = { empid: { string: '31564604' }, joindate: { date: '2017-01-01' }, nested: { nextLevel: { boolean: true } } }; 
 

 
convert(object); 
 
console.log(object);

0

如果datatype仅针对最里面的元件指定而不是由阵列或对象,然后做这样。

var json = '{"empid":{"string":"31564604"},"joindate":{"date":"2017-01-01"},"a":[{"number":1}],"level1":{"level2":{"string":"abc"}}}'; 
 

 
// parse the string 
 
var object = JSON.parse(json); 
 
updateObj(object); 
 

 
function updateObj(obj) { 
 
    // get all keys and iterate over them 
 
    Object.keys(obj).forEach(function(k) { 
 
    // get the nested object property 
 
    var key = Object.keys(obj[k])[0]; 
 
    // update only if nested property is object 
 
    typeof obj[k][key] != 'object' && (obj[k] = obj[k][key]); 
 
    // recursively call the faction if the element is object 
 
    typeof obj[k] == 'object' && updateObj(obj[k]); 
 
    }) 
 
} 
 

 
// convert back to JSON 
 
console.log(JSON.stringify(object));

+0

如果有可能删除键“字符串”无论在JSON中,并将值分配给以前键。 –

+0

@karthickRangarajan:是的,这是可能的..但如果它是一个不动产 –

+0

@karthickRangarajan:你能给我一个嵌套结构的例子吗? –

0

鉴于数据:

const oldJson = {"empid":{"string":"31564604"},"joindate":{"date":"2017-01-01"}}; 

ES6:

let newJson = {}; 
Object.keys(oldJson).forEach(key => { 
    newJson[key] = oldJson[key]; 
}); 

ES5:

var newJson = {}; 
Object.keys(oldJson).forEach(function(key) { 
    newJson[key] = oldJson[key]; 
});