将JSON从POST请求解析为数组。

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

Unmarshal json to a array from post request

问题

请看下面的翻译结果:

main.go

  1. type Data struct {
  2. unit []string `json:"unit"`
  3. }
  4. func receive(w http.ResponseWriter, r *http.Request) {
  5. dec := json.NewDecoder(r.Body)
  6. for {
  7. var d Data
  8. if err := dec.Decode(&d); err == io.EOF {
  9. break
  10. } else if err != nil {
  11. log.Println(err)
  12. }
  13. log.Printf("%s\n", d.unit)
  14. }
  15. }

抛出的错误信息为:"json: 无法将数组解组为 main.Data 类型的 GO 值"

moj.js

  1. $(function(){
  2. $('#start').on('click', function(){
  3. var i;
  4. var j = 0;
  5. for (i = 0; i < result.length; i++){
  6. if(result[i] == null){
  7. }else if(result[i]==""){
  8. }else{
  9. lookup[j] = result[i];
  10. j++
  11. }
  12. }
  13. $.ajax({
  14. type: 'POST',
  15. url: '/start',
  16. data: '[{"unit":"' + lookup + '"}]',
  17. dataType: "json",
  18. contentType: "application/json",
  19. success: function () {
  20. alert("数据已提交。")
  21. },
  22. error: function(){
  23. alert('提交数据时出错。')
  24. }
  25. });
  26. });
  27. });

我发送的 "json" 数据看起来像这样:[{"unit":"something"}]。

在控制台中,我可以看到数据已经以这种方式被提交。

英文:

see below my

main.go

  1. type Data struct {
  2. unit []string `json:&quot;unit&quot;`
  3. }
  4. func receive(w http.ResponseWriter, r *http.Request) {
  5. dec := json.NewDecoder(r.Body)
  6. for {
  7. var d Data
  8. if err := dec.Decode(&amp;d); err == io.EOF {
  9. break
  10. } else if err != nil {
  11. log.Println(err)
  12. }
  13. log.Printf(&quot;%s\n&quot;, d.unit)
  14. }
  15. }

Error that is throwed: "json: cannot unmarshal array into GO value of type main.Data"

moj.js

  1. $(function(){
  2. $(&#39;#start&#39;).on(&#39;click&#39;, function(){
  3. var i;
  4. var j = 0;
  5. for (i = 0; i &lt; result.length; i++){
  6. if(result[i] == null){
  7. }else if(result[i]==&quot;&quot;){
  8. }else{
  9. lookup[j] = result[i];
  10. j++
  11. }
  12. }
  13. $.ajax({
  14. type: &#39;POST&#39;,
  15. url: &#39;/start&#39;,
  16. data: &#39;[{&quot;unit&quot;:&quot;&#39;+lookup+&#39;&quot;}]&#39;,
  17. dataType: &quot;json&quot;,
  18. contentType: &quot;application/json&quot;,
  19. success: function () {
  20. alert(&quot;Data posted.&quot;)
  21. },
  22. error: function(){
  23. alert(&#39;Error posting data.&#39;)
  24. }
  25. });
  26. });
  27. });

The "json" that I send looks like: [{"unit":"something"}].

In the console I can see the data has been posted like this.

答案1

得分: 2

两件事情:

  1. 你正在解组一个 Data 切片,而不是一个 'unit' 切片。
  2. 将 'unit' 字段设为公开,以便通过反射对解码器可见。

参考:https://play.golang.org/p/4kfIQTXqYi

  1. type Data struct {
  2. Unit string `json:"unit"`
  3. }
  4. func receive(w http.ResponseWriter, r *http.Request) {
  5. dec := json.NewDecoder(r.Body)
  6. for {
  7. var d []Data
  8. if err := dec.Decode(&d); err == io.EOF {
  9. break
  10. } else if err != nil {
  11. log.Println(err)
  12. }
  13. log.Printf("%s\n", d.Unit)
  14. }
  15. }
英文:

Two things:

  1. You are unmarshalling a slice of Data rather than a slice of 'unit'.
  2. Make the 'unit' field public so that it is visible by reflection to the Decoder

See: https://play.golang.org/p/4kfIQTXqYi

  1. type Data struct {
  2. Unit string `json:&quot;unit&quot;`
  3. }
  4. func receive(w http.ResponseWriter, r *http.Request) {
  5. dec := json.NewDecoder(r.Body)
  6. for {
  7. var d []Data
  8. if err := dec.Decode(&amp;d); err == io.EOF {
  9. break
  10. } else if err != nil {
  11. log.Println(err)
  12. }
  13. log.Printf(&quot;%s\n&quot;, d.Unit)
  14. }
  15. }

huangapple
  • 本文由 发表于 2015年12月1日 22:30:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/34022839.html
匿名

发表评论

匿名网友

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

确定