2013-10-29 53 views
0

我目前正忙於Unity中的項目。對於這個項目,我需要使用和使用json數據。JSON數據錯誤Unity3d

這是JSON文件:它都按計劃進行,直到行

{ 
    "exerciseFrame": { 
     "currentFrameRate": 115.003, 
     "gestures": [], 
     "hands": [ 
      { 
       "direction": [ 
        -0.21788, 
        0.396045, 
        -0.892007 
       ], 
       "id": 67, 
       "palmNormal": [ 
        -0.207517, 
        -0.911865, 
        -0.354174 
       ], 
       "palmPosition": [ 
        76.5549, 
        114.137, 
        5.89759 
       ], 
       "palmVelocity": [ 
        11.5489, 
        -12.4382, 
        30.413 
       ], 
       "r": [ 
        [ 
         0.985174, 
         0.0992006, 
         -0.139971 
        ], 
        [ 
         -0.110688, 
         0.990883, 
         -0.0768105 
        ], 
        [ 
         0.131075, 
         0.0911648, 
         0.987172 
        ] 
       ], 
       "s": 0.877737, 
       "sphereCenter": [ 
        53.6198, 
        53.1508, 
        -39.351 
       ], 
       "sphereRadius": 91.0197, 
       "stabilizedPalmPosition": [ 
        74.1678, 
        112.23, 
        3.77527 
       ], 
       "t": [ 
        -20.2956, 
        10.8737, 
        19.0197 
       ], 
       "timeVisible": 3.23297 
      } 
     ], 
     "id": 470433, 
     "interactionBox": { 
      "center": [ 
       0, 
       189, 
       0 
      ], 
      "size": [ 
       209.24, 
       209.24, 
       146.232 
      ] 
     }, 
     "pointables": [ 
      { 
       "direction": [ 
        -0.204191, 
        -0.171441, 
        -0.963802 
       ], 
       "handId": 67, 
       "id": 83, 
       "length": 79.2433, 
       "stabilizedTipPosition": [ 
        54.4213, 
        125.134, 
        -95.3633 
       ], 
       "timeVisible": 1.79999, 
       "tipPosition": [ 
        58.1631, 
        128.283, 
        -96.3226 
       ], 
       "tipVelocity": [ 
        11.7388, 
        -0.426162, 
        2.39705 
       ], 
       "tool": false, 
       "touchDistance": 0.16562, 
       "touchZone": "hovering" 
      }, 
      { 
       "direction": [ 
        -0.128641, 
        0.0244301, 
        -0.99139 
       ], 
       "handId": 67, 
       "id": 25, 
       "length": 73.593, 
       "stabilizedTipPosition": [ 
        84.4969, 
        125.889, 
        -91.8182 
       ], 
       "timeVisible": 1.40869, 
       "tipPosition": [ 
        88.0132, 
        128.67, 
        -92.9798 
       ], 
       "tipVelocity": [ 
        9.78409, 
        -4.46077, 
        -10.2516 
       ], 
       "tool": false, 
       "touchDistance": 0.0726596, 
       "touchZone": "hovering" 
      }, 
      { 
       "direction": [ 
        -0.231257, 
        -0.0952694, 
        -0.968217 
       ], 
       "handId": 67, 
       "id": 62, 
       "length": 65.8749, 
       "stabilizedTipPosition": [ 
        27.6915, 
        127.768, 
        -78.6761 
       ], 
       "timeVisible": 0.913038, 
       "tipPosition": [ 
        30.0744, 
        130.094, 
        -78.9935 
       ], 
       "tipVelocity": [ 
        11.5967, 
        -2.61466, 
        -3.92538 
       ], 
       "tool": false, 
       "touchDistance": 0.123818, 
       "touchZone": "hovering" 
      }, 
      { 
       "direction": [ 
        -0.0484869, 
        0.109018, 
        -0.992857 
       ], 
       "handId": 67, 
       "id": 73, 
       "length": 46.8336, 
       "stabilizedTipPosition": [ 
        115.627, 
        114.182, 
        -61.4815 
       ], 
       "timeVisible": 0.739126, 
       "tipPosition": [ 
        118.889, 
        116.921, 
        -62.7602 
       ], 
       "tipVelocity": [ 
        2.0058, 
        -14.1922, 
        26.0571 
       ], 
       "tool": false, 
       "touchDistance": 0.308196, 
       "touchZone": "hovering" 
      }, 
      { 
       "direction": [ 
        -0.757118, 
        0.0997547, 
        -0.645617 
       ], 
       "handId": 67, 
       "id": 37, 
       "length": 47.2933, 
       "stabilizedTipPosition": [ 
        -13.0828, 
        113.28, 
        3.91602 
       ], 
       "timeVisible": 0.913038, 
       "tipPosition": [ 
        -10.3237, 
        117.652, 
        2.30821 
       ], 
       "tipVelocity": [ 
        9.14501, 
        5.11948, 
        -3.45668 
       ], 
       "tool": false, 
       "touchDistance": 0.0164196, 
       "touchZone": "hovering" 
      } 
     ], 
     "r": [ 
      [ 
       0.564536, 
       0.157925, 
       -0.81016 
      ], 
      [ 
       0.200296, 
       -0.978399, 
       -0.0511501 
      ], 
      [ 
       -0.800738, 
       -0.133395, 
       -0.583973 
      ] 
     ], 
     "s": -443.531, 
     "t": [ 
      22821.5, 
      -11650.1, 
      -3347.64 
     ], 
     "timestamp": 5463086706 
    } 
} 

我已經裝載了它與下面的腳本統一(按照本教程http://www.paultondeur.com/2010/03/23/tutorial-loading-and-parsing-external-xml-and-json-files-with-unity-part-2-json/

using UnityEngine; 
using LitJson; 
using System; 
using System.Collections; 

public class LoadJSON : MonoBehaviour 
{ 
    IEnumerator Start() 
    { 

     //Load JSON data from a URL 
     string url = "http://localhost/project/application/exercise.json"; 
     WWW www = new WWW(url); 

     //Load the data and yield (wait) till it's ready before we continue executing the rest of this method. 
     yield return www; 
     if (www.error == null) 
     {  
      //Process exercises found in JSON file  
     ProcessExercises(www.data); 
     } 
     else 
     { 
      Debug.Log("ERROR: " + www.error); 
     } 

    } 

    private void ProcessExercises(string jsonString) 
    { 
     Debug.Log (jsonString); 
     JsonData jsonExercise = JsonMapper.ToObject(jsonString); // convert json data to object. 
     Exercise exercise; 

     for(int i = 0; i<jsonExercise["exerciseFrame"].Count; i++) // for each exerciseFrame data in the .json file 
     { 
     Debug.Log(jsonExercise["exerciseFrame"].Count); 
     } 
    } 
    private void loadExercise(){ 
    } 


}` 

那應該將json數據轉換爲對象:

JsonData jsonExercise = JsonMapper.ToObject(jsonString); // convert json data to object. 

我收到以下錯誤,但我不知道錯在哪裏。因爲jsonString是一個包含數據的字符串。

`ArgumentNullException: Argument cannot be null. 
Parameter name: key 
System.Collections.Generic.Dictionary`2[System.String,LitJson.PropertyMetadata].ContainsKey (System.String key) (at /Applications/buildAgent/work/c514da0c8183631c/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:458) 
LitJson.JsonMapper.ReadValue (System.Type inst_type, LitJson.JsonReader reader) 
LitJson.JsonMapper.ReadValue (System.Type inst_type, LitJson.JsonReader reader) 
LitJson.JsonMapper.ReadValue (System.Type inst_type, LitJson.JsonReader reader) 
LitJson.JsonMapper.ReadValue (System.Type inst_type, LitJson.JsonReader reader) 
LitJson.JsonMapper.ToObject[JsonData] (System.String json) 
LoadJSON.ProcessExercises (System.String jsonString) (at Assets/Scripts/JSON/LoadJSON.cs:32) 
LoadJSON+<Start>c__Iterator1.MoveNext() (at Assets/Scripts/JSON/LoadJSON.cs:20)` 

我真的很希望這裏有人能幫助我。非常感謝您的時間!

+1

JSON看起來有效。你是否粘在LitJson上?也許值得您嘗試一個不同的json解析庫。 – dwerner

+0

嘿!你認爲LitJson是問題嗎?對不同圖書館有什麼建議? – Shark

+1

統一社區已經回答了這個問題:http://answers.unity3d.com/questions/8044/what-is-the-easiest-json-library-to-integrate-with.html – dwerner

回答

0

我發現問題是什麼。每個變量的值也應該是一個字符串。因此,在這種情況下,例如

   "direction": [ 
       -0.21788, 
       0.396045, 
       -0.892007 
      ], 

應該是:

   "direction": [ 
       "-0.21788", 
       "0.396045", 
       "-0.892007" 
      ], 
+0

json允許'數字'值,這不是'json問題',這是'LibJson問題'。快速試用fastJSON,速度快,工作正常。 http://www.codeproject.com/Articles/159450/fastJSON – afpro