收到的 JSON 切断了开头
Posted
技术标签:
【中文标题】收到的 JSON 切断了开头【英文标题】:Received JSON cuts off the beginning 【发布时间】:2014-04-08 09:21:35 【问题描述】:我在使用 C# 的 Unity 项目中收到 JSON
数组。但是,它有时会在读取过程中丢失数组的第一部分。
这是读取它的代码:
while(sck.Connected)
byte[] buffer = new byte[100000];
int rec = sck.Receive(buffer, 0, buffer.Length, 0);
Array.Resize(ref buffer, rec);
text = Encoding.Default.GetString(buffer);
当接收到以下数组时,它以某种方式丢失了第一部分。
[
"id": 27,
"question": "What color is #FF0000 in Hexadecimal",
"answers": [
"answer": "red",
"correct": 1
,
"answer": "blue",
"correct": 0
,
"answer": "yellow",
"correct": 0
,
"answer": "green",
"correct": 0
]
,
"id": 80,
"question": "Which language is the native language in Uruguay? ",
"answers": [
"answer": "Portuguese",
"correct": 0
,
"answer": "Uruguese",
"correct": 0
,
"answer": "English",
"correct": 0
,
"answer": "Spanish",
"correct": 1
]
,
"id": 109,
"question": "What World War II operation was code named 'Barbarossa'?",
"answers": [
"answer": "Invasion of Africa",
"correct": 0
,
"answer": "Invasion of The Soviet Union ",
"correct": 1
,
"answer": "Invasion of France",
"correct": 0
,
"answer": "Invasion of Denmark",
"correct": 0
]
,
"id": 118,
"question": "The cassowary and Emu are both flightless birds of which country? ",
"answers": [
"answer": "South Africa",
"correct": 0
,
"answer": "India",
"correct": 0
,
"answer": "Brazil",
"correct": 0
,
"answer": "Australia",
"correct": 1
]
,
"id": 13,
"question": "10-12*4=?",
"answers": [
"answer": "-38",
"correct": 1
,
"answer": "38",
"correct": 0
,
"answer": "8",
"correct": 0
,
"answer": "-8",
"correct": 0
]
,
"id": 96,
"question": "What is not a type of knitted fabric?",
"answers": [
"answer": "Terry",
"correct": 0
,
"answer": "Jersey",
"correct": 0
,
"answer": "Rib",
"correct": 0
,
"answer": "Sateen",
"correct": 1
]
,
"id": 33,
"question": "Which movie involves worm holes, teenagers and a bunny rabbit?",
"answers": [
"answer": "Lord of the Rings",
"correct": 0
,
"answer": "Clueless",
"correct": 0
,
"answer": "Donnie Darko",
"correct": 1
,
"answer": "Star Wars",
"correct": 0
]
,
"id": 99,
"question": "Which musician did not die at the age of 27?",
"answers": [
"answer": "Kurt Cobain",
"correct": 0
,
"answer": "Amy Winehouse",
"correct": 0
,
"answer": "Janis Joplin",
"correct": 0
,
"answer": "Freddie Mercury",
"correct": 1
]
,
"id": 54,
"question": "What was the apex predator in the 1993 film \"Jurassic Park\"?",
"answers": [
"answer": "Tyrannosaurus ",
"correct": 1
,
"answer": "Dilophosauridae",
"correct": 0
,
"answer": "Denver",
"correct": 0
,
"answer": "Velociraptor",
"correct": 0
]
,
"id": 49,
"question": "Who is serving breakfast for Rory Mcllroy?",
"answers": [
"answer": "Caroline Wocniacki",
"correct": 1
,
"answer": "Anna Kournikova",
"correct": 0
,
"answer": "Serena Williams",
"correct": 0
,
"answer": "Miss Piggy",
"correct": 0
]
]
数组在||
处被截断
"answer":"Clueless","co||rrect":0,"answer":"Donnie Darko","correct":1
但是,它可以很好地处理这个array
:
[
"id": 121,
"question": "How many miles do the teams race in the Oxford and Cambridge boat race ? ",
"answers": [
"answer": "4",
"correct": 1
,
"answer": "1",
"correct": 0
,
"answer": "2",
"correct": 0
,
"answer": "3",
"correct": 0
]
,
"id": 61,
"question": "How many different Formula 1 teams has Kimi Raikkonen driven for up to 2014?",
"answers": [
"answer": "2",
"correct": 0
,
"answer": "6",
"correct": 0
,
"answer": "4",
"correct": 1
,
"answer": "10",
"correct": 0
]
,
"id": 57,
"question": "Which muppet is Swedish?",
"answers": [
"answer": "Miss Piggy",
"correct": 0
,
"answer": "Chef",
"correct": 1
,
"answer": "Yodel",
"correct": 0
,
"answer": "Gonzo",
"correct": 0
]
,
"id": 49,
"question": "Who is serving breakfast for Rory Mcllroy?",
"answers": [
"answer": "Caroline Wocniacki",
"correct": 1
,
"answer": "Anna Kournikova",
"correct": 0
,
"answer": "Serena Williams",
"correct": 0
,
"answer": "Miss Piggy",
"correct": 0
]
,
"id": 116,
"question": "What is the highest active volcano in Europe ? ",
"answers": [
"answer": "Vesuvius",
"correct": 0
,
"answer": "Eyjafjallajökull",
"correct": 0
,
"answer": "The sky mountain",
"correct": 0
,
"answer": "Etna",
"correct": 1
]
,
"id": 56,
"question": " In 2013 How many people lived china",
"answers": [
"answer": "0,8 billions",
"correct": 0
,
"answer": "0,6 billions",
"correct": 0
,
"answer": "2,0 billions",
"correct": 0
,
"answer": "1,3 billions",
"correct": 1
]
,
"id": 47,
"question": "You have 3 red cars, 42 yellow cars and 21 green cars. How many cars are left if you half all the yellow cars and double the green cars.",
"answers": [
"answer": "42",
"correct": 0
,
"answer": "66",
"correct": 1
,
"answer": "3",
"correct": 0
,
"answer": "63",
"correct": 0
]
,
"id": 28,
"question": "If you mix blue and red you will get",
"answers": [
"answer": "Green",
"correct": 0
,
"answer": "Orange",
"correct": 0
,
"answer": "Purple",
"correct": 1
,
"answer": "Pink",
"correct": 0
]
,
"id": 141,
"question": "Which spirit is used in a Bloody Mary?",
"answers": [
"answer": "Vodka",
"correct": 1
,
"answer": "Rum",
"correct": 0
,
"answer": "Gin",
"correct": 0
,
"answer": "Whisky",
"correct": 0
]
,
"id": 45,
"question": "Which character in the TV series \"Game of Thrones\" is the centre of lust for many males around the world?",
"answers": [
"answer": "Queen Freelus",
"correct": 0
,
"answer": "Daenerys Targaryen",
"correct": 1
,
"answer": "Princess Liea",
"correct": 0
,
"answer": "Madam Butterfly",
"correct": 0
]
]
我做错了什么?
【问题讨论】:
JSON 本身是有效的。所以一定是代码或者连接。 【参考方案1】:您的缓冲区似乎不够大。
byte[] buffer = new byte[100000];
我会根据您对代码的需求研究几个不同的选项
-
增加缓冲区的大小
使用memory stream
Asynchronous Server Socket
【讨论】:
以上是关于收到的 JSON 切断了开头的主要内容,如果未能解决你的问题,请参考以下文章
如何从Typescript中以#或@开头的属性获取JSON数据
错误域 = NSCocoaErrorDomain 代码 = 3840 “JSON 文本不是以数组或对象开头