2015-07-10 21 views
1

對不起,我的英語:)Unity SimpleJSON數組延遲

在我的項目中,我使用SimpleJSON。 我有這個json字符串 這是一個星球在我的遊戲列表。 我需要解析這個json。 但我有1個問題。 我的Unity編輯器凍結!!!當我使用循環。

{ 
"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); 
} 


我統一編輯器是凍結!爲什麼?

回答

1

我想通:
我用:
for (int i = 0; i <= arr.Count; i++)
arr.Count == 2
需要使用此:for (int i = 0; i < arr.Count; i++)
更換<=<
做工精細。