i have been searching in internet while api convert json tabular format. don't have code tried. please direct me if have idea it.
eg : json
{"name":"rinu","age":"14","phone":[{"countrycode":91,"number":"99862656"},{"countrycode":91,"number":"675432"}],"otherdetails":[{"active":true}]}
output can be(with separated)
rinu|14|91|99862656|true rinu|14|91|675432|true i don't want ready-made stuff, if similar this, can re-write it.
you might need this:
jacksonread.java
import java.io.bufferedwriter; import java.io.file; import java.io.filewriter; import java.io.ioexception; import org.codehaus.jackson.map.objectmapper; public class jacksonread { public static void main(string[] args) { objectmapper mapper = new objectmapper(); try { example example = mapper.readvalue(new file("d:\\user.json"), example.class); stringbuilder builder = new stringbuilder(); int = 0; (phone phone : example.getphone()) { builder.append(example.getname()).append("|"); builder.append(example.getage()).append("|"); builder.append(phone.getcountrycode()).append("|") .append(phone.getnumber()).append("|") .append(example.getotherdetails().get(i).getactive()) .append("|"); builder.append("\n"); } file file = new file("d:\\user.txt"); // if file doesnt exists, create if (!file.exists()) { file.createnewfile(); } filewriter fw = new filewriter(file.getabsolutefile()); bufferedwriter bw = new bufferedwriter(fw); bw.write(builder.tostring()); bw.close(); } catch (ioexception e) { e.printstacktrace(); } } } example.java
import java.util.arraylist; import java.util.list; import org.codehaus.jackson.annotate.jsonproperty; public class example { @jsonproperty("name") private string name; @jsonproperty("age") private string age; @jsonproperty("phone") private list<phone> phone = new arraylist<phone>(); @jsonproperty("otherdetails") private list<otherdetail> otherdetails = new arraylist<otherdetail>(); /** * * @return name */ @jsonproperty("name") public string getname() { return name; } /** * * @param name * name */ @jsonproperty("name") public void setname(string name) { this.name = name; } /** * * @return age */ @jsonproperty("age") public string getage() { return age; } /** * * @param age * age */ @jsonproperty("age") public void setage(string age) { this.age = age; } /** * * @return phone */ @jsonproperty("phone") public list<phone> getphone() { return phone; } /** * * @param phone * phone */ @jsonproperty("phone") public void setphone(list<phone> phone) { this.phone = phone; } /** * * @return otherdetails */ @jsonproperty("otherdetails") public list<otherdetail> getotherdetails() { return otherdetails; } /** * * @param otherdetails * otherdetails */ @jsonproperty("otherdetails") public void setotherdetails(list<otherdetail> otherdetails) { this.otherdetails = otherdetails; } @override public string tostring() { return "example [name=" + name + ", age=" + age + ", phone=" + phone + ", otherdetails=" + otherdetails + "]"; } } phone.java
import org.codehaus.jackson.annotate.jsonproperty; public class phone { @jsonproperty("countrycode") private integer countrycode; @jsonproperty("number") private string number; /** * * @return countrycode */ @jsonproperty("countrycode") public integer getcountrycode() { return countrycode; } /** * * @param countrycode * countrycode */ @jsonproperty("countrycode") public void setcountrycode(integer countrycode) { this.countrycode = countrycode; } /** * * @return number */ @jsonproperty("number") public string getnumber() { return number; } /** * * @param number * number */ @jsonproperty("number") public void setnumber(string number) { this.number = number; } @override public string tostring() { return "phone [countrycode=" + countrycode + ", number=" + number + "]"; } } otherdetail.java
import org.codehaus.jackson.annotate.jsonproperty; public class otherdetail { @jsonproperty("active") private boolean active; /** * * @return active */ @jsonproperty("active") public boolean getactive() { return active; } /** * * @param active * active */ @jsonproperty("active") public void setactive(boolean active) { this.active = active; } @override public string tostring() { return "otherdetail [active=" + active + "]"; } } user.json
{"name":"rinu","age":"14","phone":[{"countrycode":91,"number":"99862656"},{"countrycode":91,"number":"675432"}],"otherdetails":[{"active":true}]}
Comments
Post a Comment