Golang如何在json.RawMessage中修改值

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

Golang changing value in json.RawMessage

问题

我有一个json.RawMessage,为了获取它,我需要向API发送一个请求。我的问题是,我需要对这个消息进行更改,只需要更改一个字段,所以我的问题是最有效的方法是什么?

  1. json.RawMessage
  2. {
  3. "make": "VW",
  4. "model": "ARTEON",
  5. "version": "2.0 TDI",
  6. "year_min": 2017,
  7. "power_ps": 200,
  8. "fuel": "diesel",
  9. "body_type": "sedan",
  10. "currency": "EUR",
  11. "co2_emission": 130
  12. }

例如,我想将燃料类型从柴油更改为汽油。

  1. 期望的输出
  2. {
  3. "make": "VW",
  4. "model": "ARTEON",
  5. "version": "2.0 TDI",
  6. "year_min": 2017,
  7. "power_ps": 200,
  8. "fuel": "gasoline",
  9. "body_type": "sedan",
  10. "currency": "EUR",
  11. "co2_emission": 130
  12. }
英文:

I have json.RawMessage and for getting I need to send a request to API. My problem is that I need to make a change in that message I just need to change one field, so my question is what is the most effective way to do it

  1. json.RawMessage
  2. {
  3. "make": "VW",
  4. "model": "ARTEON",
  5. "version": "2.0 TDI",
  6. "year_min": 2017,
  7. "power_ps": 200,
  8. "fuel": "diesel",
  9. "body_type": "sedan",
  10. "currency": "EUR",
  11. "co2_emission": 130
  12. }

so for example I want to change fuel type from diesel to gasoline

  1. expected output
  2. {
  3. "make": "VW",
  4. "model": "ARTEON",
  5. "version": "2.0 TDI",
  6. "year_min": 2017,
  7. "power_ps": 200,
  8. "fuel": "gasoline",
  9. "body_type": "sedan",
  10. "currency": "EUR",
  11. "co2_emission": 130
  12. }

答案1

得分: 0

除了@mkopriva的评论(我认为这是更可取的方式),你可以尝试使用map而不是struct(如果struct对你不适用)。

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. var rm = json.RawMessage(`{
  7. "make": "VW",
  8. "model": "ARTEON",
  9. "version": "2.0 TDI",
  10. "year_min": 2017,
  11. "power_ps": 200,
  12. "fuel": "diesel",
  13. "body_type": "sedan",
  14. "currency": "EUR",
  15. "co2_emission": 130
  16. }`)
  17. func main() {
  18. var objmap map[string]interface{}
  19. err := json.Unmarshal(rm, &objmap)
  20. if err != nil {
  21. panic(err)
  22. }
  23. objmap["fuel"] = "gasoline"
  24. result, err := json.Marshal(objmap)
  25. if err != nil {
  26. panic(err)
  27. }
  28. fmt.Println(string(result))
  29. }

https://go.dev/play/p/nue-SA-LGVf

英文:

In addition to the comment from @mkopriva(That I think is the more preferable way), you can try using a map instead of a struct(If a struct is not applicable to you).

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. var rm = json.RawMessage(`{
  7. "make": "VW",
  8. "model": "ARTEON",
  9. "version": "2.0 TDI",
  10. "year_min": 2017,
  11. "power_ps": 200,
  12. "fuel": "diesel",
  13. "body_type": "sedan",
  14. "currency": "EUR",
  15. "co2_emission": 130
  16. }`)
  17. func main() {
  18. var objmap map[string]interface{}
  19. err := json.Unmarshal(rm, &objmap)
  20. if err != nil {
  21. panic(err)
  22. }
  23. objmap["fuel"] = "gasoline"
  24. result, err := json.Marshal(objmap)
  25. if err != nil {
  26. panic(err)
  27. }
  28. fmt.Println(string(result))
  29. }

https://go.dev/play/p/nue-SA-LGVf

huangapple
  • 本文由 发表于 2022年1月27日 21:51:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/70879713.html
匿名

发表评论

匿名网友

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

确定