简体   繁体   English

Xamarin&Golang-{[]}无效字符'\\ x00'寻找对象密钥字符串的开头

[英]Xamarin & Golang - { []} invalid character '\x00' looking for beginning of object key string

I'm comming today because I'm stuck and it doesn't seems logic for me. 我今天要来是因为我被卡住了,这对我来说似乎不合逻辑。

I have my server ( Go ) and my smartphone app ( Xamarin C# ). 我有我的服务器( Go )和我的智能手机应用程序( Xamarin C# )。

For the Xamarin side, I'm using this package -> Sockets Plugin for Xamarin and Windows (PCL) 对于Xamarin,我正在使用此程序包-> Xamarin和Windows(PCL)的套接字插件

For the Go side, I'm using encoding/json 对于Go端,我正在使用encoding / json

Everything in the Xamarin part is working fine. Xamarin部分的所有功能都正常运行。 But in the Go side, it doesn't.. I'm using the following code to handle the messages from each net.Conn . 但是在Go方面,它没有..我正在使用以下代码来处理来自每个net.Conn的消息。

type DialMessageContainer struct {
    Type string `json:"Type"`
    Content json.RawMessage `json:"Content"`
}

func (dialTCP *DialTCP) serve_conn(conn net.Conn) {

    fmt.Println("Handle new connection.")
    dec := json.NewDecoder(conn)
    var message m.DialMessageContainer

    //Use one of the following
    printContent(conn)                                                                                                                                                                                 
    // --
    err := dec.Decode(&message)
    fmt.Println(message, err)
    //End

    conn.Close()
}

func printContent(conn net.Conn) {
    var buf bytes.Buffer
    io.Copy(&buf, conn)
    fmt.Println(buf.String())
}

So, the type is here to let it know which type of json it is. 因此,这里的类型是让它知道它是哪种json类型。 From this type string, it then unmarshal a json from the json.RawmMessage Content to the good object. 然后,从该类型字符串中将json.RawmMessage内容中的json解组到好的对象。 I know it works, however when I try to recreate my object from the following json, I get this error: 我知道它有效,但是当我尝试从以下json重新创建对象时,出现此错误:

(printContent no commented, then Decode has nothing to read, it's for the debug trace test) (printContent没有注释,然后Decode没有任何内容可供读取,用于调试跟踪测试)

....
Handle new connection.
{
  "Type": "Authentication",
  "Content": {
    "UUID": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6Im1heGltZS1ndWl0dGV0QG91dGxvb2suY29tIiwiZXhwIjoxNDYyNjM4MTkzfQ.DEGJcDYl9Iq4nayo37Rq9ZsK8mrU-V8gU5I8JLO8oLg"
  }
}
{ []} EOF

So, when I hide the printContent(conn) , the dec.Decode gives this: 因此, 当我隐藏printContent(conn)时 ,dec.Decode给出了这一点:

....
Handle new connection.
{ []} invalid character '\x00' looking for beginning of object key string

The thing that I ask to myself is : "Does \\x00 means the NULL CHAR of the ascii table?". 我对自己问的是:“ \\ x00是否表示ascii表的NULL CHAR?”。 If this error comes because of this char, so where does it is? 如果此错误是由于此字符而引起的,那么它在哪里呢? Moreover, if it's this char, maybe it is just present to mark the end point of the string (use of '\\0' in C to mark the end of a char*/char[]). 而且,如果是此char,则可能只是为了标记字符串的结束点而存在(在C中使用'\\ 0'来标记char * / char []的结束)。

But, maybe I'm totaly wrong.. Honestly I'm stuck and I don't understand.. Aynway, JSONLint says it's a valid JSON so it can only be something about the reading I think 但是,也许我完全错了。.老实说,我被卡住了,我听不懂.. Aynway, JSONLint说这是有效的JSON,所以只能是我认为的阅读内容

If anyone can help, thank ! 如果有人可以帮助,谢谢!

您的printConn读取消息,然后dec.Decode(&message)没什么dec.Decode(&message)

I found what was the problem... 我发现了问题所在...

To exchange messages between my server/client I use JSON form. 要在服务器/客户端之间交换消息,我使用JSON形式。


The server is making in Go and uses the encoding/json package. 服务器Go中制作,并使用encoding/json包。

The client is a Xamarin Form Application in C# and uses Newtonsoft.json package. 客户端C#中Xamarin表单应用程序,并使用Newtonsoft.json包。


Once a JSON is serialized, it takes form as a string. JSON序列化后,将采用字符串形式。 However to write/read between client/server, it has to be formatted as bytes ( C# byte[] && Go []byte ), so we have to convert the jsonString into a bytes array. 但是,要在客户端/服务器之间进行写入/读取,必须将其格式化为字节( C# byte[] && Go []byte ),因此我们必须将jsonString转换为bytes数组。

Take a look at this conversion with my Pseudo Emixam23 : 用我的Pseudo Emixam23看一下这种转换:

// C#
Emixam23 == [69][0][109][0][105][0][120][0][97][0][109][0][50][0][51][0]

// Go
Emixam23 == [69][109][105][120][97][109][50][51]

The two array represents the byte array for our string Emixam23 , however, they are differents. 这两个数组代表我们的字符串Emixam23的字节数组,但是它们是不同的。 As you can see, we have a [0] for the C# part, unlike the Go part. 如您所见,与Go部分不同,C#部分具有[0] This [0] is the sign of the byte at left . [0]是左边字节的符号

A byte which equals 0 means '\\0' and then, the end of a string for a language as C. I think that Go works same. 等于0的字节表示'\\0' ,然后是C语言的字符串的结尾。我认为Go的工作原理相同。 If I'm right then the error is logic, when json.Decode() //go iterate over my byte array, then it goes to the end which is '\\0' . 如果我是对的,那么错误就是逻辑,当json.Decode() //go遍历我的字节数组时,它会到达结尾'\\0' So decode stops itself at the 2nd case of my byte array and try to create a JSON with this string "{" which is an invalid JSON. 因此,解码会在字节数组的第二种情况下停止运行,并尝试使用此字符串"{"创建一个JSON,这是无效的JSON。

Of course, it does the same for the C# part. 当然,对于C#部分也是如此。 I then created these two functions which sign() and unsign() a byte array. 然后,我创建了这两个函数,它们对字节数组进行sign()unsign()

// for both, bytes[] is the byte array you want to convert and
// the lenght of this byte array when you call the function
public byte[] UnsignByteArray(byte[] bytes, int lenght)
    {
        int index = 0;
        int i = 0;
        var array = new byte[lenght / 2];

        while (index < lenght)
        {
            array[i] = bytes[index];
            index += 2;
            i++;
        }

        return array;
    }
public byte[] SignByteArray(byte[] bytes, int lenght)
    {
        int index = 0;
        int i = 0;
        var array = new byte[lenght * 2];

        while (index < lenght)
        {
            array[i] = bytes[index];
            i++;
            index++;
            array[i] = 0;
            i++;
        }

        return array;
    }

Never forget to take a look with Debug/Printing every data for both side, it can help ! 永远不要忘记调试/打印双方的每个数据,它会有所帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JSON无效字符&#39;}&#39;正在寻找对象键字符串的开头 - JSON invalid character '}' looking for beginning of object key string json 格式无效,请检查。 原因:寻找对象键字符串开头的无效字符“a” - Invalid json format, please check. Reason: invalid character 'a' looking for beginning of object key string “顶级值后的无效字符&#39;\\ x00&#39;” - “invalid character '\x00' after top-level value” JSON int密钥发布数据e2e(寻找对象密钥字符串开头的无效字符“ 1”) - JSON int key issue data e2e (invalid character '1' looking for beginning of object key string) Golang无效字符&#39;b&#39;寻找值的开始 - Golang invalid character 'b' looking for beginning of value JSON无法写入创世纪块:无效字符&#39;\\\\&#39;寻找对象密钥字符串的开头 - JSON failed to write genesis block: invalid character '\\' looking for beginning of object key string 服务器错误 (BadRequest):寻找 object 键字符串开头的无效字符“\\” - Error from server (BadRequest): invalid character '\\' looking for beginning of object key string “无效字符&#39;u&#39;寻找值的开始”使用golang开发的服务解析错误 - “invalid character 'u' looking for beginning of value”Parsing Error from an Service developed in golang golang websocket.JSON.Receive无效字符&#39;q&#39;寻找值的开头 - golang websocket.JSON.Receive invalid character 'q' looking for beginning of value Python json.loads 失败并出现错误 - “simplejson.errors.JSONDecodeError: Invalid control character &#39;\\x00&#39; at: line 1 column 536 (char 535)” - Python json.loads fails with error- "simplejson.errors.JSONDecodeError: Invalid control character '\x00' at: line 1 column 536 (char 535)"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM