-
-
Notifications
You must be signed in to change notification settings - Fork 202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to avoid the HTML characters replacing with unicode characters #103
Comments
Hey @kaarthickkaran, thanks for trying out gabs! The issue you bumped into is due to the way in which the standard Go JSON lib marshals JSON as documented here: https://golang.org/pkg/encoding/json/#Marshal
Here is one way to work around this: package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"github.com/Jeffail/gabs/v2"
)
func main() {
jsonObj := gabs.New()
jsonObj.SetP("protein", "product.name")
jsonObj.SetP("eg.<this product>", "product.description")
jsonObj.SetP(1000, "product.price")
buf := bytes.Buffer{}
encoder := json.NewEncoder(&buf)
encoder.SetEscapeHTML(false)
encoder.SetIndent("", " ")
if err := encoder.Encode(jsonObj.Data()); err != nil {
log.Fatalf("Failed to encode: %s", err)
return
}
fmt.Println(buf.String())
} The output of the above code is:
Hope this helps! |
Hi @mihaitodor Sir, Thank you so much for the solution sir. Let me try this. |
If anybody is seeing escaped UTF-8 characters like this when trying to extract a string from parsed JSON, please try the following: // escapes string
value := parsedJSON.Path("value").String()
// does not escape string
value, ok := parsedJSON.Path("value").Data().(string) |
Hi,
am new to GO, I want to form a JSON for an item, so using this module I formed the JSON, but the angular brackets were replaced with Unicode characters. I want to know how to avoid the replacement of the Unicode character.
Code:
package main
import (
"fmt"
"github.com/Jeffail/gabs/v2"
)
func main() {
jsonObj := gabs.New()
jsonObj.SetP("protein", "product.name")
jsonObj.SetP("eg.", "product.description")
jsonObj.SetP(1000, "product.price")
fmt.Println(jsonObj.StringIndent("", " "))
}
Actual Output:
{
"product": {
"description": "eg.\u003cthis product \u003e ",
"name": "protein",
"price": 1000
}
}
Expected Output:
{
"product": {
"description": "eg.<this product>",
"name": "protein",
"price": 1000
}
}
The text was updated successfully, but these errors were encountered: