获取 Json 数据并单独解析数据(c# unity)

Get Json Data and parse the data individually (c# unity)

我使用此代码

成功地将我的字符串数据转换为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 data

这里有更多信息代码

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_ ];

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

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

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

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

o.GetComponent<UISprite>().spriteName = indivisualChar == "1" ? "layout_player_bigline-01" : "layout_banker_bigline-01";