在http响应中将任意字符串编码为JSON

I have a string of JSON that I want to encode as json into an http response.

This returns a string in the response:

str := "{\"key1\":{\"key2\":\"value1\",\"key3\":\"value2\"}}"
err := json.NewEncoder(w).Encode(str)

I'm trying to first marshal the string to JSON. Which gives me another string of random bytes.

str := "{\"key1\":{\"key2\":\"value1\",\"key3\":\"value2\"}}"
js, _ := json.Marshal(str)
err := json.NewEncoder(w).Encode(js)
查看全部
dongmubi4444
dongmubi4444
2015/05/27 17:25
  • json
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复