How to convert Properties class to JSON Object using JAVA -


can me? how convert below input json object?

input :

{ "details": { "device/0/endpointclientname": "ndm-xx-1", "device/1/endpointclientname": "ndm-xx-2", "energymeter/0/current": "20", "energymeter/0/total": "400", } } 

output:-

{  "device": [ {"endpointclientname":"ndm-xx-1" }, {"endpointclientname":"ndm-xx-2" } ], "energymeter": [ {"current":"20", "total":"400"} ] }  

i have input json object properties class. in input sharing full path. have convert json object.

[demo]https://jsfiddle.net/cntchen/vh7kat5a/

   var input = {       "details": {         "device/0/endpointclientname": "ndm-xx-1",         "device/1/endpointclientname": "ndm-xx-2",         "energymeter/0/current": "20",         "energymeter/0/total": "400",       }     };      function decodeflatobj(flatojb) {       var outputobj = {};       (var key in flatojb) {         var objnow = outputobj;         var subkey = key.split('/');          (var = 0; < subkey.length - 1; i++) {           // next subkey number           if (/\d|[1-9]\d*/.test(subkey[i + 1])) {             // current subkey number             if (/\d|[1-9]\d*/.test(subkey[i])) {               objnow.push([]);               objnow = objnow[parseint(subkey[i])];             } else {               objnow[subkey[i]] = objnow[subkey[i]] || [];               objnow = objnow[subkey[i]];             }           } else { // next subkey object             // current subkey number             if (/\d|[1-9]\d*/.test(subkey[i])) {               objnow[parseint(subkey[i])] = objnow[parseint(subkey[i])] || {};               objnow = objnow[parseint(subkey[i])];             } else {               objnow[subkey[i]] = objnow[subkey[i]] || {};               objnow = objnow[subkey[i]];             }           }         }          var valuedecode;         if (typeof flatojb[key] === 'object') {           valuedecode = decodeflatobj(flatojb[key]);         } else {           valuedecode = flatojb[key];         }          if (/\d|[1-9]\d*/.test(subkey[subkey.length - 1])) {           objnow[parseint(subkey[subkey.length - 1])].push(valuedecode);         } else {           objnow[subkey[subkey.length - 1]] = valuedecode;         }       }        return outputobj;     }      var output = decodeflatobj(input);     console.log(input);     console.log(json.stringify(output));     //{"details":{"device":[{"endpointclientname":"ndm-xx-1"},{"endpointclientname":"ndm-xx-2"}],"energymeter":[{"current":"20","total":"400"}]}} 

Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -