0

I got nested JSON code like below, what looks like a tree, having multi-layer child node. So how unmarshall it in Go? Anyone has a good idea?

{
   "name": "mark",
   "sons": [
     {
       "name": "markson1"
     },
     {
       "name": "markson2",
       "sons": [
         {
           "name": "markson21",
           "sons": [
             {
               "name": "markson211"
             }
           ]
         }
       ]
     }
   ]
}

sorry for bothering you, I just resolved the problem by using recursive call. anyway, thx all.

here is my code:

func Unmarshal(j string) error {
    obj, err := simplejson.NewJson([]byte(j))
    if err != nil {
        return err
    }
    foo := make(map[string]byte)
    bar, err := recursion(foo, obj);
    if err != nil {
        return err
    }
    bar[obj.Get("name").MustString()]=0
    fmt.Println(bar)
    return nil
}

func recursion(foo map[string]byte, obj *simplejson.Json) (map[string]byte, error) {
    if exist(obj) {
        sonsObj := obj.Get("sons")
        sons, err := sonsObj.Array()
        if err != nil {
            return nil, err
        }
        for key, _ := range sons {
            obj = sonsObj.GetIndex(key)
            n := obj.Get("name").MustString()
            foo[n]=0
            tmp, err := recursion(foo, obj)
            if err != nil {
                return nil, err
            }
            for k, _ := range tmp {
                foo[k]=0
            }
        }
    }
    return foo, nil
}

func exist(obj *simplejson.Json) bool {
    _, exist := obj.CheckGet("sons")
    return exist
}

output:
map[mark:0 markson1:0 markson2:0 markson21:0 markson211:0]
--- PASS: TestUnmarshal (0.00s)
--- PASS: TestUnmarshal/#00 (0.00s)
PASS

Zander Wong
  • 639
  • 2
  • 8
  • 15
  • What have you tried? Include your code. What specific problem are you having? – Jonathan Hall Sep 25 '19 at 14:49
  • Create a matching structure using structs and slices, you can start with a flat node (step 1), then add a slice of nodes (step 2), and you're done. – mkopriva Sep 25 '19 at 14:52
  • 1
    Go and the JSON library both support structs defined recursively, i.e. the struct has a field whose type is the struct itself. – Jessie Sep 25 '19 at 15:39
  • 1
    @Flimzy The provided duplicate is not the same problem -- nested payloads vs. recursive payloads are two different things. – Jessie Sep 25 '19 at 15:41
  • @Jesse: JSON doesn't permit recursion, and the question doesn't show recursion. So where are you getting that recursion is related? – Jonathan Hall Sep 25 '19 at 15:44
  • If there's something that the duplicate question doesn't answer, the OP is invited (in the automatically added text) to submit a new question. But the options for _this_ question are to close it as a duplicate, or close it as off-topic for not including the code to be debugged. Closing it as off-topic seems the more charitable, and most likely to help. – Jonathan Hall Sep 25 '19 at 15:51
  • how about this? `res := map[string]interface{}{}` -> `json.Unmarshal(jsondata, &res)` [Playground](https://play.golang.org/p/rtwwGF1CJyi). Be more specific on describe you problem please – Rama Bramantara Sep 25 '19 at 16:13
  • 2
    Not a duplicate, and is recursive. https://play.golang.org/p/OWVdsH8BfpO – virullius Sep 25 '19 at 16:34
  • @Jesse thx for help – Zander Wong Sep 25 '19 at 18:33
  • @mjb2kmn thx. i got it. – Zander Wong Sep 25 '19 at 18:34
  • @Flimzy yes you r right. i should post my code first – Zander Wong Sep 25 '19 at 18:36
  • @RamaBramantara sorry for my pool english. actually, i wanna get everyname in the json. Jesse is right, it is a recursive problem. – Zander Wong Sep 25 '19 at 18:40
  • @mkopriva yep, that's another way. thx – Zander Wong Sep 25 '19 at 18:40

0 Answers0