问题
I successfully converted my string data to json by using this code
//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;
}
Now I am making this scoreboard that uses NGUI and what I am trying to achieve is to do it like this
Originally i want to make like this scoreboard
Now what i did so far is like this
[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;
}
What I am getting with this code is this
And also it is all red its like not parsing my jsonString really well. But it successfully get my 12 jsonString data
here are more information code
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_ ];
Maybe can someone help me please with my problem.
回答1:
I did it like this. On my //loop over it
statement
for (int i = 1; i < obj.dataToParse.Length - 1; i += 3)
{
char indivisualChar = obj.dataToParse[i].toString();
and changed my condition from o.GetComponent<UISprite>().spriteName = indivisualChar == 1 ? "layout_player_bigline-01" : "layout_banker_bigline-01";
to this
o.GetComponent<UISprite>().spriteName = indivisualChar == "1" ? "layout_player_bigline-01" : "layout_banker_bigline-01";
来源:https://stackoverflow.com/questions/49913711/get-json-data-and-parse-the-data-individually-c-unity