获取Json数据并分别解析数据(C#统一) [英] Get Json Data and parse the data individually (c# unity)
问题描述
我使用此代码成功地将字符串数据转换为json
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;
}
现在,我正在制作使用NGUI的记分牌,而我想要实现的目标就是像这样
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
它也全是红色,就像没有很好地解析我的jsonString一样.但是它成功获取了我的12个jsonString数据
And also it is all red its like not parsing my jsonString really well. But it successfully get my 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_ ];
也许有人可以帮助我解决我的问题.
Maybe can someone help me please with my problem.
推荐答案
我是这样做的.在我的//loop over it
声明
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();
并将我的状况从o.GetComponent<UISprite>().spriteName = indivisualChar == 1 ? "layout_player_bigline-01" : "layout_banker_bigline-01";
更改为此
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";
这篇关于获取Json数据并分别解析数据(C#统一)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!