Home > OS >  return json parsed output in for loop in golang
return json parsed output in for loop in golang

Time:10-09

i want to return the below json response output from this function, so i can use that returned value in another function. but in the for loop i cant able to do that is there any solution for this.

func Search_num(mobilenumber string) {

    //.......get request.......

    body, err := io.ReadAll(search_res.Body)
    if err != nil {
        log.Fatalln(err)
    }
    defer search_res.Body.Close()

    var jsonbody Response

    if err := json.Unmarshal(body, &jsonbody); err != nil {
        log.Fatalln(err)
    }

    for _, p := range jsonbody.Data {
        output := fmt.Sprintf("Name : %s\nMobile : %s\nEmail : %s\nCarrier : %s\nCity : %s\n", p.Name, p.Phone.Mobile, p.INTaddress.Email, p.Phone.Carrier, p.Address.City)

        
    }

    return output

}

seen some solutions but didnt understood how to assign the below json fmt.Sprintf("Name : %s\nMobile : %s\nEmail : %s\nCarrier : %s\nCity : %s\n", p.Name, p.Phone.Mobile, p.INTaddress.Email, p.Phone.Carrier, p.Address.City) in a slice :|

CodePudding user response:

like this, output not use := in the for range, just use = 。 and for loop sees not need, just you demand use jsonbody.Data[0] or jsonbody.Data[len(jsonbody.Data)-1]

func Search_num(mobilenumber string) {

//.......get request.......

body, err := io.ReadAll(search_res.Body)
if err != nil {
    log.Fatalln(err)
}
defer search_res.Body.Close()

var jsonbody Response

if err := json.Unmarshal(body, &jsonbody); err != nil {
    log.Fatalln(err)
}

var output string
for _, p := range jsonbody.Data {
    output = fmt.Sprintf("Name : %s\nMobile : %s\nEmail : %s\nCarrier : %s\nCity : %s\n", p.Name, p.Phone.Mobile, p.INTaddress.Email, p.Phone.Carrier, p.Address.City)
    
}

return output

}

CodePudding user response:

If I get this correctly you have a json of the format:

[
  {"name" : "name1", "mobile": "123", "email": "[email protected]", "carrier": "carrier1", "city", "city1"},
  {"name" : "name2", "mobile": "1234", "email": "[email protected]", "carrier": "carrier2", "city", "city2"}
  ...
]

which you would like to search through, find the number (eg 1234) and return the whole struct formatted using your Sprintf

If your Response struct is like:

type Response struct {
  Name string
  Mobile string
  Email string
  Carrier string
  City string
}

Then you need to provide a slice of those into the Unmarshaler to get them back. Like:

func Search_num(mobilenumber string) string {

    //.......get request.......

    body, err := io.ReadAll(search_res.Body)
    if err != nil {
        log.Fatalln(err)
    }
    defer search_res.Body.Close()

    var responses []Response

    if err := json.Unmarshal(body, &responses); err != nil {
        log.Fatalln(err)
    }

    for _, p := range responses {
        if p.Mobile == mobilenumber {
            return fmt.Sprintf("Name : %s\nMobile : %s\nEmail : %s\nCarrier : %s\nCity : %s\n", p.Name, p.Mobile, p.Email, p.Carrier, p.City)
        }
    }

    return mobilenumber " not found"
}
  • Related