0

How can I convert json (gson or jackson, not important) to a json object with recursion elements? Example of json: (each objects values can contain another object values)

{
  "values": [
    {
      "name": "Name 1",
      "values": [
        {
          "name": "Name 1.1"
        }
      ]
    },
    {
      "name": "Name 2"
    }
  ]
}

I want to convert to a class of a kind

public class Simple {
    public List<ValuesEntity> values;

    public static class ValuesEntity {
        public String name;
        public List<ValuesEntity> values;
    }
}
Uxname
  • 1
  • 1
  • What errors have you gotten when trying anything? – OneCricketeer Jul 12 '17 at 15:26
  • Please note: Your inner objects are missing `"values"` keys – OneCricketeer Jul 12 '17 at 15:27
  • Yes, inner objects may not contain `"values"` keys. > What errors have you gotten when trying anything? I don't know how to go through all entries in json when it have recursive items. Its not just loop – Uxname Jul 12 '17 at 15:30
  • You don't need a loop yourself to serialize/deserialize. Gson and Jackson can convert your data within 3-5 lines of code... – OneCricketeer Jul 12 '17 at 15:34
  • Even when json file size about 50mb ? – Uxname Jul 12 '17 at 15:35
  • I don't see why size really matters. You may get an OOM exception, but it's just a file parser. – OneCricketeer Jul 12 '17 at 15:38
  • if you really need to keep the memory footprint as low as possible, give a try to http://rapidjson.org/md_doc_sax.html (java version: https://github.com/s0cks/RapidJson-Java) which to be like SAX but for JSON. But your algorithm will be very different (conceptually) from a jackson-based. – spi Jul 12 '17 at 15:46
  • Check this post. Could be useful to you. https://stackoverflow.com/questions/4373233/is-there-a-saxparser-that-reads-json-and-fires-events-so-it-looks-like-xml – Anil Jul 12 '17 at 16:23

0 Answers0