0

我目前正忙于 Unity 的一个项目。对于这个项目,我需要使用和处理 json 数据。

这是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)`

我真的希望这里有人可以帮助我。非常感谢您的时间!

4

1 回答 1

0

我发现了问题所在。每个变量的值也应该是一个字符串。所以在这种情况下,例如

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

应该:

                "direction": [
                "-0.21788",
                "0.396045",
                "-0.892007"
            ],
于 2013-10-31T11:57:47.457 回答