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

时间:2018-04-19 05:57:14

标签: c# android json unity3d ngui

我使用此代码

成功将我的字符串数据转换为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的记分牌,而我想要实现的就是这样做

enter image description here

最初我想像这个记分牌一样

enter image description here

到目前为止,我所做的就像这样

[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;
}

我使用此代码得到的是

enter image description here

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

enter image description here

这里有更多信息代码

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

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

1 个答案:

答案 0 :(得分:0)

我是这样做的。在我的//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";