在golang中以JSON格式获取x-www-form-urlencoded请求的嵌套键值对。

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

Get nested key value pair of x-www-form-urlencoded request in JSON format in golang

问题

我有一个使用情况,我们在 x-www-form-urlencoded 的请求体中获取到了嵌套的键值对,如下所示:

name=abc&age=12&notes[key1]=value1&notes[key2]=value2

我尝试使用 url.ParseQuery("name=abc&age=12&notes\[key1\]=value1&notes\[key2\]=value2"),但它返回的结果是:

{
  "name": "abc",
  "age": 12,
  "notes[key1]": "value1",
  "notes[key2]": "value2"
}

我该如何将上述请求体的值转换为以下的 JSON 格式:

{
  "name": "abc",
  "age": 12,
  "notes": {
    "key1": "value1",
    "key2": "value2"
  }
}

有可能 notes 的嵌套层级可能达到 3 层。

我已经尝试过 url.ParseQueryr.Form,但它们都只返回了 notes[key1]notes[key2]

英文:

I have use case where we are getting nested key value in x-www-form-urlencoded body like below

name=abc&age=12&notes[key1]=value1&notes[key2]=value2

I tried url.ParseQuery("name=abc&age=12&notes\[key1\]=value1&notes\[key2\]=value2") but it's giving

{
  "name": "abc",
  "age": 12,
  "notes[key1]": "value1",
  "notes[key2]": "value2"
}

How can I get this value in below JSON format for above body

{
  "name": "abc",
  "age": 12,
  "notes": {
    "key1": "value1",
    "key2": "value2"
  }
}

There is possibility that notes might be in 3 level nested format

I have tried url.ParseQuery and r.Form but both of them giving notes[key1] and notes[key2].

答案1

得分: 1

要解析具有此类型的查询字符串参数名称的嵌套键值,可以使用derekstavis/go-qs,它是Rack查询字符串解析器的一个端口。

这将返回一个带有嵌套键值的map[string]interface{},如下所示。

值得注意的是,age的值返回为字符串,但是url.ParseQuery也是如此。如果希望将其编组为整数,可以分叉并修改该软件包。

{
  "age": "12",
  "name": "abc",
  "notes": {
    "key1": "value1",
    "key2": "value2"
  }
}

在Go Playground上有一个完整的示例,代码如下:

Go Playground URL: https://go.dev/play/p/Px7uZWRNs5V

package main

import (
	"encoding/json"
	"fmt"
	"log"

	qs "github.com/derekstavis/go-qs"
)

func main() {
	v := "name=abc&age=12&notes[key1]=value1&notes[key2]=value2"

	q, err := qs.Unmarshal(v)
	if err != nil {
		log.Fatal(err)
	}

	j, err := json.MarshalIndent(q, "", "  ")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(string(j))
}
英文:

To unmarshal nested key values with this type of query string parameter names, it's possible to use derekstavis/go-qs which is a port of Rack's query string parser.

This returns a map[string]interface{} with nested key values as follows.

> Of note, the value for age is returned as a string, however, this is the same for url.ParseQuery. If marshaling this to an integer is desired, the package can be forked and modified.

{
  "age": "12",
  "name": "abc",
  "notes": {
    "key1": "value1",
    "key2": "value2"
  }
}

A complete example is available on the Go Playground with the following code:

Go Playground URL: https://go.dev/play/p/Px7uZWRNs5V

package main

import (
	"encoding/json"
	"fmt"
	"log"

	qs "github.com/derekstavis/go-qs"
)

func main() {
	v := "name=abc&age=12&notes[key1]=value1&notes[key2]=value2"

	q, err := qs.Unmarshal(v)
	if err != nil {
		log.Fatal(err)
	}

	j, err := json.MarshalIndent(q, "", "  ")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(string(j))
}

huangapple
  • 本文由 发表于 2023年6月24日 15:06:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76544961.html
匿名

发表评论

匿名网友

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

确定