Reputation: 271
I have a NodeJS Server and a C# Unity Client. The NodeJS server sends an object to the C# client.
SERVER
var obj = new Object();
obj.name = data.name;
obj.position = data.position;
var jsonString= JSON.stringify(obj);
C# Client
try
{
Debug.Log(response.ToString()); // WORKS the result is
/*
[
{
"name": "Peter",
"position": "(13.6, 1.5, 2.3)"
}
]
*/
Debug.Log(response.GetValue(1).ToString()); // Don't work, receive in console (ERROR).
}
catch
{
Debug.Log("Error");
}
Output
[
{
"name": "Peter",
"position": "(13.6, 1.5, 2.3)"
}
]
So I try to read the JSON to get the Value Name and Position.
I have already tried the following:
string resVal1 = response.GetValue<string>();
Enemy resVal2 = response.GetValue<Enemy>(1);
string enemyName= response.GetValue(1).Value<string>("name");
Source: https://github.com/doghappy/socket.io-client-csharp
I also only get "Error" here. What am I doing wrong? How do I get name and position from the JSON string. I have not much experience with JSON.
Upvotes: 0
Views: 456
Reputation: 136124
The JSON returned is an array with a single element, that element is an object with 2 properties - one of which is name
so I suspect he correct code to read that would be:
string enemyName= response.GetValue(0).Value<string>("name");
Upvotes: 1