I am currently using facebook and I have a callback method that returns a JSON result as a string.
The result has the following format:
{
"request": "420211088059698",
"to": [
"100002669403922",
"100000048490273"
]
}
How would I go about parsing the "to" into a list of some sort? This way I can use this list to verify that the user actually did indeed send a request to a friend to play the game.
Thanks guys
You need to convert it to a class array. That can be done with Unity's built in JsonUtility API.
JsonUtility.ToJson
to convert a class to Json.
JsonUtility.FromJson
to convert Json back to class.
Visit here Json array example.
EDIT:
You asked for an example:
class FacebookInfo
{
public string request;
public string[] to;
}
void Start()
{
FacebookInfo fbInfo = new FacebookInfo();
string fbJson = "{\"request\": \"420211088059698\",\"to\": [\"100002669403922\",\"100000048490273\"]}";
fbInfo = JsonUtility.FromJson<FacebookInfo>(fbJson);
//Show request
Debug.Log("Request: " + fbInfo.request);
//Show to arrays
for (int i = 0; i < fbInfo.to.Length; i++)
{
Debug.Log("To : " + fbInfo.to[i]);
}
}
Tested with Unity 5.4.0.13B and it looks like Unity now support Json array without writing extra code. Just make sure you have this version I mentioned.
You can use JSON.Net for deserializing to a class of below structure:
Class Test{
public string request {get;set;}
public List<string> to {get;set;}
}
then just call deserialize method on the JSON string to get the object.
Test obj = JsonConvert.DeserializeObject<Test>(jsonstring);
只需使用SimpleJson http://wiki.unity3d.com/index.php/SimpleJSON ,无需额外的工作,看一下示例,您所要做的就是使用JSON.Parse,您将得到一个数组,可以像数据一样使用它[ “ request”]以获取值,希望对您有所帮助。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.