Unity SimpleJSON数组滞后
本文关键字:滞后 数组 SimpleJSON Unity | 更新日期: 2023-09-27 18:01:52
对不起,我的英语不好:)
在我的项目我使用SimpleJSON。我有这个json字符串这是我游戏中的行星列表。我需要解析这个json。但我有一个问题。我的Unity编辑器冻结了!!当我使用cycle.
{
"system_list":
[
{
"system_id":"9",
"galaxy":"1",
"x":"3",
"y":"2",
"system_name":"bla bla"
},
{
"system_id":"10"
"galaxy":"1",
"x":"1",
"y":"4",
"system_name":"NoIQ"}
]
}
解析代码如下:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using SimpleJSON;
public class GetJsonList : MonoBehaviour
{
string str_json = "{'"system_list'":[{'"system_id'":'"9'",'"galaxy'":'"1'",'"x'":'"3'",'"y'":'"2'",'"system_name'":'"bla bla'"},{'"system_id'":'"10'",'"galaxy'":'"1'",'"x'":'"1'",'"y'":'"4'",'"system_name'":'"NoIQ'"}]}";
JSONNode N;
JSONArray arr;
List<SystemPlanet> sysPlanet = new List<SystemPlanet>();
void Start()
{
N = JSON.Parse(str_json);
arr = N["system_list"].AsArray;
Debug.Log(arr[0]["system_id"].AsInt);
Debug.Log(arr[1]["system_id"].AsInt);
for (int i = 0; i <= arr.Count; i++)
{
sysPlanet.Add(new SystemPlanet(arr[i]["system_id"].AsInt, arr[i]["galaxy"].AsInt, arr[i]["x"].AsInt,
arr[i]["y"].AsInt, arr[i]["system_name"].Value));
}
Debug.Log(sysPlanet[1].sys_name);
}
}
public class SystemPlanet
{
public int sys_id;
public int galaxy;
public int x;
public int y;
public string sys_name;
public SystemPlanet(int _sys_id,int _galaxy, int _x, int _y, string _sys_name)
{
sys_id = _sys_id;
galaxy = _galaxy;
x = _x;
y = _y;
sys_name = _sys_name;
}
}
如果我使用这个Debug.Log(arr[0]["system_id"].AsInt);
或此Debug.Log(arr[1]["system_id"].AsInt);
工作好。
但如果我使用循环-这:
for (int i = 0; i <= arr.Count; i++)
{
sysPlanet.Add(new SystemPlanet(arr[i]["system_id"].AsInt, arr[i]["galaxy"].AsInt, arr[i]["x"].AsInt, arr[i]["y"].AsInt, arr[i]["system_name"].Value));
}
或者这个循环:
for (int i = 0; i <= arr.Count; i++)
{
Debug.Log("System ID: "+arr[i]["system_id"].AsInt +"'nGalaxy:"+ arr[i]["galaxy"].AsInt+"'n X: " + arr[i]["x"].AsInt +"'n Y: "+ arr[i]["y"].AsInt +"'nSystem Name: "+ arr[i]["system_name"].Value);
}
我的Unity编辑器冻结了!!为什么?
我想:
我使用:for (int i = 0; i <= arr.Count; i++)
当arr.Count == 2
需要使用这个:for (int i = 0; i < arr.Count; i++)
将<=
替换为<
工作好。