java - Transform json recursively -
i novice in java , looking way transform json recursively,i using library json.org andi have tried many things without success .
if know , in advance.i not know how code
here json:
“dates”:{ “date”:“2017-08-05”, “data”:[ “date”:{ “date”:“2017-08-07”, “values”:[ {“value”:78}, { “value”:56 } ] }, { “value”:57 } ] } the output need :
“dates”:{ “date”:“2017-08-05”, “data”:[ “date”:{ “date”:“2017-08-07”, “values”:[ { "value”:78 } // valeur 1 separated ] }, “data”:[ “date”:{ “date”:“2017-08-07”, “values”:[ { “value”:56 }// valeur 2 separated ] }, {“value”:57 } ] } i novice in java , looking way transform json recursively,i using library json.org andi have tried many things without success .
if know , in advance.i not know how code
here code not good:
package fr.aneo.spagobi.elasticsearch; import java.util.iterator; import java.util.vector; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; /** * corrector of elasticsearch array buckets * * @author cadam * */ public class corrector { private static vector<string> parent = new vector<string>(); private static string location = ""; /** * method deep explore json * * @param json * json explore * @return json * @throws jsonexception */ public static string fix(string json) throws jsonexception { jsonobject jsonobj = new jsonobject(json); @suppresswarnings("rawtypes") iterator iterator = jsonobj.keys(); // loop iterate on fields while (iterator.hasnext()) { string key = (string) iterator.next(); // if jsonobject, recall function if (jsonobj.get(key).getclass().equals(jsonobject.class)) { system.out.println("hash"); parent.add(json); fix(jsonobj.get(key).tostring()); parent.remove(parent.size() - 1); // iterate on array , recall function } else if (jsonobj.get(key).getclass().equals(jsonarray.class)) { system.out.println("array"); parent.add(json); // change arrays after 2 first branches if (parent.size() > 2) { jsonarray jsonarr = jsonobj.getjsonarray(key); // change arrays more 1 value if (jsonarr.length() > 1) { location = key; // deep dive array's values (int = 0; < jsonarr.length(); i++) { fix(jsonarr.get(i).tostring()); } // fix array fixarray(jsonarr); } } parent.remove(parent.size() - 1); // value } else { system.out.println("value"); } } return json; } /** * method transform array json object * * @param array * json array * @return json object * @throws jsonexception */ private static jsonobject fixarray(jsonarray array) throws jsonexception { system.out.println("fix :" + parent.get(parent.size() - 2).tostring()); jsonobject object = new jsonobject(parent.get(parent.size() - 2)); // system.out.println("\tparent :" + parent.get(parent.size() - 2)); // system.out.println("\tarray :" + array.tostring()); object.put(location, array.get(0)); // system.out.println("\tmodif :" + object.tostring()); return object; } }
Comments
Post a Comment