我使用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
}
]
}
]
我做错了什么?
答案 0 :(得分:1)
看起来你的缓冲区不够大。
byte[] buffer = new byte[100000];
我会根据您对代码的需求来研究几个不同的选项