Golang handling illegal characters in JSON

Source: Internet
Author: User

Original:

Hi there,

I just discovered go and decided to port a little program to Go.

The program reads json-data a URL and process the Data. The Go
Port works well till now.

I dont has any influence on the JSON data and so sometimes there is
control character in it and my program crashes with "invalid character
' \x12 ' in string literal "

Here's the code sample of my program:

Http_return, err: =var http_body []byteif err = = Nil   {=  Ioutil. ReadAll (Http_return. Body)   Http_return. Body.close ()} param_info:= Make (map[string]interface{}) Param_err:



This unmarshal call crashes with "invalid character ' \x12 ' in string
Literal

How does I remove this or any similar control character from the JSON
Data. I dont need those characters so they can simply be removed.

Thanks
Nico

==============================================================

This means that JSON is obtained by means of a network transmission or other method, and then it may contain special characters, so that the JSON parsing in go will be an error.

So one of the buddies came up with a trick.

========================

If you don ' t mind them being replaced with spaces, you could do something like:
 forI, ch: =Range Http_body {Switch {   CaseCH >'~': http_body[i] =' '   Casech = ='\ r':   Casech = ='\ n':   Casech = ='\ t':   CaseCh <' ': http_body[i] =' '  }}
What is the specific principle of ~k========================================? It's not clear. It is probably based on the character set to determine which are the illegal characters in JSON, and then replace them with spaces. Computer, character set ..... We need to look into it later. =================================== look at the ASCII to display the character, you see. Space begins, ~ ends. Plus \r\n\t these 3 characters. ASCII to display characters
Binary
decimal hexadecimal Graphics
0010 0000 32 20 (space) (?)
0010 0001 33 21st !
0010 0010 34 22 "
0010 0011 35 23 #
0010 0100 36 24 $
0010 0101 37 25 %
0010 0110 38 26 &
0010 0111 39 27
0010 1000 40 28 (
0010 1001 41 29 )
0010 1010 42 2 A *
0010 1011 43 2 b +
0010 1100 44 2 C ,
0010 1101 45 The -
0010 1110 46 2E .
0010 1111 47 2F /
0011 0000 48 30 0
0011 0001 49 31 1
0011 0010 50 32 2
0011 0011 51 33 3
0011 0100 52 34 4
0011 0101 53 35 5
0011 0110 54 36 6
0011 0111 55 37 7
0011 1000 56 38 8
0011 1001 57 39 9
0011 1010 58 3 A :
0011 1011 59 3 b ;
0011 1100 60 3C <
0011 1101 61 3D =
0011 1110 62 3E >
0011 1111 63 3F ?
Binary
decimal hexadecimal Graphics
0100 0000 64 40 @
0100 0001 65 41 A
0100 0010 66 42 B
0100 0011 67 43 C
0100 0100 68 44 D
0100 0101 69 45 E
0100 0110 70 46 F
0100 0111 71 47 G
0100 1000 72 48 H
0100 1001 73 49 I
0100 1010 74 4 A J
0100 1011 75 4 b K
0100 1100 76 4C L
0100 1101 77 4D M
0100 1110 78 4E N
0100 1111 79 4F O
0101 0000 80 50 P
0101 0001 81 51 Q
0101 0010 82 52 R
0101 0011 83 53 S
0101 0100 84 54 T
0101 0101 85 55 U
0101 0110 86 56 V
0101 0111 87 57 W
0101 1000 88 58 X
0101 1001 89 59 Y
0101 1010 90 5 A Z
0101 1011 91 5B [
0101 1100 92 5C \
0101 1101 93 5D ]
0101 1110 94 5E ^
0101 1111 95 5F _
Binary
decimal hexadecimal Graphics
0110 0000 96 60 `
0110 0001 97 61 A
0110 0010 98 62 B
0110 0011 99 63 C
0110 0100 100 64 D
0110 0101 101 65 E
0110 0110 102 66 F
0110 0111 103 67 G
0110 1000 104 68 H
0110 1001 105 69 I
0110 1010 106 6A J
0110 1011 107 6B K
0110 1100 108 6C L
0110 1101 109 6D M
0110 1110 110 6E N
0110 1111 111 6F O
0111 0000 112 70 P
0111 0001 113 71 Q
0111 0010 114 72 R
0111 0011 115 73 S
0111 0100 116 74 T
0111 0101 117 75 U
0111 0110 118 76 V
0111 0111 119 77 W
0111 1000 120 78 X
0111 1001 121 79 Y
0111 1010 122 7A Z
0111 1011 123 7B {
0111 1100 124 7C |
0111 1101 125 7D }
0111 1110 126 7E ~

Golang handling illegal characters in JSON

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.