在Unity C#中使用JSON MiniJSON反序列化 [英] Deserialization of JSON using MiniJSON in Unity C#

查看:2804
本文介绍了在Unity C#中使用JSON MiniJSON反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表的项目使用WWW从PHP文件发送到统一。该WWW.text看起来像<$c$c>[{\"playerId\":\"1\",\"playerLoc\":\"Powai\"},{\"playerId\":\"2\",\"playerLoc\":\"Andheri\"},{\"playerId\":\"3\",\"playerLoc\":\"Churchgate\"}]在那里我修剪多余的 [] 从字符串。当我尝试使用Boomlagoon.JSON解析它,只有第一个对象被检索。我发现我有反序列化()列表,并有进口MiniJSON。

I have a list of items send from a PHP file to unity using WWW. The WWW.text looks like [{"playerId":"1","playerLoc":"Powai"},{"playerId":"2","playerLoc":"Andheri"},{"playerId":"3","playerLoc":"Churchgate"}] where I trim the extra [] from the string. When I try to parse it using Boomlagoon.JSON, only the first object is retrieved. I found out that I have to deserialize() the list and have imported MiniJSON.

不过,我很困惑如何反序列化()这个列表。我想通过每一个JSON对象循环和检索数据。我如何使用C#这样做是团结?

But I am confused how to deserialize() this list. I want to loop through every JSON object and retrieve data. How can I do this in Unity using C#?

我使用的类

public class player {

    public string playerId { get; set; }
    public string playerLoc { get; set; }
    public string playerNick { get; set; }
}

修剪 [] 之后我能够使用解析的MiniJSON JSON。但它仅返回第一个 KeyValuePair <​​/ code>。

After trimming the [] I am able to parse the json using MiniJSON. But it is returning only the first KeyValuePair.

        IDictionary<string,object> s = Json.Deserialize(serviceData) as IDictionary<string,object>;
        foreach (KeyValuePair<string, object> kvp in s)
        {
            Debug.Log(string.Format("Key = {0}, Value = {1}", kvp.Key, kvp.Value));
        }

谢谢!

推荐答案

团结加入 JsonUtility 他们的API后的 5.3.3 更新。忘掉所有的第三方库。 JsonUtility超过 10倍 更快比其他的JSON库。更新至统一的 5.3.3 或以上版本,然后尝试以下code。

Unity added JsonUtility to their API after 5.3.3 Update. Forget about all the 3rd party libraries. JsonUtility is more than 10x faster than other Json libraries. Update to Unity 5.3.3version or above then try the code below.

要您的播放器类转换为的Json ,然后将其序列化回班的没有使用的第三方库:

To convert your player class to Json then serialize it back to class without using third-party libraries:

1。一个数据(NON ARRAY JSON)

public class player
{

    public string playerId;
    public string playerLoc;
    public string playerNick;
}

public class APP : MonoBehaviour
{
    player playerInstance;

    void Start()
    {
        playerInstance = new player();
        playerInstance.playerId = "8484239823";
        playerInstance.playerLoc = "Powai";
        playerInstance.playerNick = "Random Nick";

        //Convert to Jason
        string playerToJason = JsonUtility.ToJson(playerInstance);
        Debug.Log("Jason is " + playerToJason);

        //Convert Json Back to Class(Serialize) METHOD 1 
        player serializedPlayer = new player();
        serializedPlayer = JsonUtility.FromJson<player>(playerToJason);
        //serializedPlayer now conatains data from playerToJason/playerInstance

        //Convert Json Back to Class(Serialize) METHOD 2 (Creates new instance of player. No new keyword required)
        player AnotherSerializedPlayer = (player)JsonUtility.FromJson(playerToJason, typeof(player));
        //AnotherSerializedPlayer now conatains data from playerToJason/playerInstance

        //Convert Json Back to Class(Serialize) METHOD 3(Overwrite the existing   class  instance "playerInstance". Less memory Allocation)
        JsonUtility.FromJsonOverwrite(playerToJason, playerInstance);
    }
}

修改

2。多数据(ARRAY JSON)
您的JSON包含多个数据。例如 playerId 比出现的一次更多。 Unity的 JsonUtility 不支持阵列,因为它仍然是新的,但你可以使用的帮助类从这个人得到的阵列有工作 JsonUtility

2. MULTIPLE DATA(ARRAY JSON) Your Json contains multiple data. For example playerId appeared more than once. Unity's JsonUtility does not support array as it is still new but you can use a helper class from this person to get array working with JsonUtility.

创建一个名为类 JsonHelper 。直接从下面复制JsonHelper。

Create a class called JsonHelper. Copy the JsonHelper directly from below.

using System;
using UnityEngine;
using System.Collections;

public static class JsonHelper
{
    public static T[] FromJson<T>(string json)
    {
        Wrapper<T> wrapper = UnityEngine.JsonUtility.FromJson<Wrapper<T>>(json);
        return wrapper.Items;
    }

    public static string ToJson<T>(T[] array)
    {
        Wrapper<T> wrapper = new Wrapper<T>();
        wrapper.Items = array;
        return UnityEngine.JsonUtility.ToJson(wrapper);
    }

    [Serializable]
    private class Wrapper<T>
    {
        public T[] Items;
    }
}

您原始数据是

 [{"playerId":"1","playerLoc":"Powai"},{"playerId":"2","playerLoc":"Andheri"},{"playerId":"3","playerLoc":"Churchgate"}]

添加 {项目:在它的以及添加 } 在它的结束即可。

Add {"Items": in front of it then add } at the end of it.

code要做到这一点:

Code to do this:

serviceData = "{\"Items\":" + serviceData + "}";

现在您有:

 {"Items":[{"playerId":"1","playerLoc":"Powai"},{"playerId":"2","playerLoc":"Andheri"},{"playerId":"3","playerLoc":"Churchgate"}]}

连载多个从PHP数据的阵列,你现在可以做的。

To serialize the multiple data from php as arrays, you can now do

public player[] playerInstance;
playerInstance = JsonHelper.FromJson<player>(serviceData);

playerInstance [0] 是您的第一个数据

playerInstance [1] 是你的第二数据

playerInstance [2] 是你的第三个数据

或使用类中的数据 playerInstance [0] .playerLoc playerInstance [1] .playerLoc playerInstance [2] .playerLoc ......

or data inside the class with playerInstance[0].playerLoc, playerInstance[1].playerLoc, playerInstance[2].playerLoc ......

您可以使用 playerInstance.Length 访问它之前检查的长度。

You can use playerInstance.Length to check the length before accessing it.

注:删除 {搞定;组; } 播放类。如果你有 {搞定;组; } ,它不会工作。 Unity的 JsonUtility 确实不会与定义为类成员工作的性质

NOTE: Remove { get; set; } from the player class. If you have { get; set; }, it wont work. Unity's JsonUtility does NOT work with class members that are defined as properties.

这篇关于在Unity C#中使用JSON MiniJSON反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆