【问题标题】:Get Json Data and parse the data individually (c# unity)获取 Json Data 并单独解析数据(c# unity)
【发布时间】:2018-04-19 05:57:14
【问题描述】:

我使用此代码成功地将我的字符串数据转换为 json

//1 = blue circle, 2 = red circle
string jsonString = "[1, 1, 1, 2, 1, 1, 1, 2, 1, 2, 1, 1, 1, 2, 1, 1]"; //sample data

ExampleClass dataParser = new ExampleClass();
dataParser.dataToParse = jsonString;

//Convert to Json
string exampleClassToJson = JsonUtility.ToJson(dataParser);
Debug.Log(exampleClassToJson);

ExampleClass obj = JsonUtility.FromJson<ExampleClass>(exampleClassToJson);

//Loop over it
for (int i = 1; i < obj.dataToParse.Length - 1; i += 3)
{
   char indivisualChar = obj.dataToParse[i];
}

[Serializable]
public class ExampleClass
{
   public string dataToParse;
}

现在我正在制作这个使用 NGUI 的记分牌,我想要实现的就是这样做

本来我也想做这样的记分牌

现在我做的事情是这样的

[SerializeField] protected GameObject prefab_big_road = null;

[SerializeField] Transform pos_big_road = null;

string jsonString = "[1, 1, 1, 2, 1, 1, 1, 2, 1, 2, 1, 1, 1, 2, 1, 1]"; //sample data

int[] array_big_road = tzPlayInfo.Instance._BIG_ROAD_;
private void Start()
{
    WinLog();
}

IEnumerator WinLog_big_road()
{
    ExampleClass dataParser = new ExampleClass();
    dataParser.dataToParse = jsonString;

    //Convert to Json
    string exampleClassToJson = JsonUtility.ToJson(dataParser);
    Debug.Log(exampleClassToJson);

    ExampleClass obj = JsonUtility.FromJson<ExampleClass>(exampleClassToJson);

    DeleteChildrens(pos_big_road);

    //Loop over it
    for (int i = 1; i < obj.dataToParse.Length - 1; i += 3)
    {
        char indivisualChar = obj.dataToParse[i];

        yield return new WaitForEndOfFrame();

        int j = 1;

        if (j < rh.Const._HISTORY_COUNT_ * rh.Const._HISTORY_HEIGHT_)
        {

            GameObject o = Instantiate(prefab_big_road) as GameObject;
            o.transform.SetParent(pos_big_road);
            o.transform.localScale = Vector3.one;
            int x = j % rh.Const._HISTORY_COUNT_;
            int y = j / rh.Const._HISTORY_COUNT_;
            float xl = 2.3f;
            float yl = -26.69f;
            o.transform.localPosition = new Vector3(x * xl, y * yl, 0f);
            o.GetComponent<UISprite>().spriteName = indivisualChar == 1 ? "layout_player_bigline-01" : "layout_banker_bigline-01";
            NGUITools.SetActive(o, true);
            j++;
            yield return new WaitForEndOfFrame();
        }
    }
    yield break;
}


void DeleteChildrens(Transform t)
{
    NGUITools.DestroyChildren(t);
}

public void WinLog()
{
    StopCoroutine("WinLog_big_road");
    StartCoroutine("WinLog_big_road");
}
}
[Serializable]
public class ExampleClass
{
    public string dataToParse;
}

这段代码我得到的是这个

而且它都是红色的,就像没有很好地解析我的 jsonString 一样。但它成功获取了我的 12 个 jsonString 数据

这里有更多信息代码

ConstantValue.cs

public const int _HISTORY_COUNT_ = 70;
public const int _HISTORY_HEIGHT_ = 6;

PlayInfo.cs

public int[] _BIG_ROAD_ = new int[Const._HISTORY_COUNT_ * Const._HISTORY_HEIGHT_ ];

也许有人可以帮我解决我的问题。

【问题讨论】:

  • 有人请帮帮我:(

标签: c# android json unity3d ngui


【解决方案1】:

我是这样做的。在我的//loop over it 声明中

for (int i = 1; i < obj.dataToParse.Length - 1; i += 3)
{
    char indivisualChar = obj.dataToParse[i].toString();

并将我的条件从 o.GetComponent&lt;UISprite&gt;().spriteName = indivisualChar == 1 ? "layout_player_bigline-01" : "layout_banker_bigline-01"; 更改为此

o.GetComponent&lt;UISprite&gt;().spriteName = indivisualChar == "1" ? "layout_player_bigline-01" : "layout_banker_bigline-01";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多