return json parsed output in for loop in golang

huangapple go评论79阅读模式
英文:

return json parsed output in for loop in golang

问题

你想从这个函数中返回下面的JSON响应output,以便在另一个函数中使用返回的值。但是在for循环中你无法这样做,有没有解决办法呢?

我看到了一些解决方案,但不太清楚如何将下面的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)赋值给一个切片。

英文:

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 return json parsed output in for loop in golang

答案1

得分: 2

像这样,在for循环中不使用:=,而是使用=。并且for循环看起来是不需要的,只需根据需要使用jsonbody.Data[0]或jsonbody.Data[len(jsonbody.Data)-1]。

func Search_num(mobilenumber string) {

//.......获取请求.......

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("姓名:%s\n手机号:%s\n电子邮件:%s\n运营商:%s\n城市:%s\n", p.Name, p.Phone.Mobile, p.INTaddress.Email, p.Phone.Carrier, p.Address.City)
    
}

return output

}

英文:

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

}

答案2

得分: 0

如果我理解正确,你有一个格式为json的数据:

[
  {"name" : "name1", "mobile": "123", "email": "email1@example.com", "carrier": "carrier1", "city", "city1"},
  {"name" : "name2", "mobile": "1234", "email": "email2@example.com", "carrier": "carrier2", "city", "city2"}
  ...
]

你想要在这个数据中搜索,找到对应的号码(例如1234),并返回整个结构,使用你的Sprintf进行格式化。

如果你的Response结构体如下所示:

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

那么你需要将它们提供给Unmarshaler来获取它们。像这样:

func Search_num(mobilenumber string) string {

    //.......获取请求.......

    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"
}
英文:

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

[
  {"name" : "name1", "mobile": "123", "email": "email1@example.com", "carrier": "carrier1", "city", "city1"},
  {"name" : "name2", "mobile": "1234", "email": "email2@example.com", "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"
}

huangapple
  • 本文由 发表于 2022年10月9日 18:30:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/74003977.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定