【问题标题】:How to correctly get array of array from Json in Unity如何在 Unity 中正确地从 Json 获取数组数组
【发布时间】:2021-08-03 21:30:10
【问题描述】:

我拥有一系列公司的 JSON。他们有一系列带有描述和产品名称的产品。在 Unity 中,我可以看到除描述和产品名称之外的所有内容。产品类别有问题吗?

我有这个 JSON:

{
"company": [{
    "companyName": "samsung",       
    "products": [
        [{
            "productName": "samusung1",
            "productDescription": "description1"
        }],
        [{
            "productName": "samusung2",
            "productDescription": "description2"
        }],
        [{
            "productName": "samusung3",
            "productDescription": "description3"
        }],
        [{
            "productName": "samusung4",
            "productDescription": "description4"
        }]
    ]
}]

}

Unity 中的类:

public class JsonReader : MonoBehaviour
{
public TextAsset companyJson;
[System.Serializable]
public class Company
{
    public string companyName;
    public Products[] products;
}

[System.Serializable]
public class Products
{
    public string productName;
    public string productDescription;
}
[System.Serializable]
public class CompanyList
{
    public Company[] company;
}

public CompanyList companyList = new CompanyList();

void Start()
{
    companyList = JsonUtility.FromJson<CompanyList>(companyJson.text);
} 
}

【问题讨论】:

    标签: c# json unity3d


    【解决方案1】:

    您的每件商品,例如

    {
        "productName": "samusung4",
        "productDescription": "description4"
    }
    

    在您的 json 中由于某种原因嵌套在单个数组中

    [ 
        {
            "productName": "samusung4",
            "productDescription": "description4"
        }
    ]
    

    在您的 JSON 中,products 不是产品数组,而是产品数组的数组。

    所以你的 JSON 应该看起来像

    {
    "company": [{
        "companyName": "samsung",       
        "products": [
            {
                "productName": "samusung1",
                "productDescription": "description1"
            },
            {
                "productName": "samusung2",
                "productDescription": "description2"
            },
            {
                "productName": "samusung3",
                "productDescription": "description3"
            },
            {
                "productName": "samusung4",
                "productDescription": "description4"
            }
        ]
    }]
    }
    

    或者你的类结构需要看起来像

    [System.Serializable]
    public class Company
    {
        public string companyName;
        public Products[][] products;
    }
    
    [System.Serializable]
    public class Products
    {
        public string productName;
        public string productDescription;
    }
    
    [System.Serializable]
    public class CompanyList
    {
        public Company[] company;
    }
    

    但 Unity 内置 JsonUtility 不支持多维数组。您需要使用像 Newtonsoft .Net JSON 这样的第三方库(在较新版本中通过包管理器有一个 Unity Package)。


    无论如何,第一个解决方案(不同的 JSON)似乎更有意义。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多