You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Unity JsonUtility序列化含嵌套对象的JSON失败求助

Unity中JsonUtility无法正确序列化API返回的JSON数据

API返回的JSON格式

{    
    "success": true,    
    "results": 1,    
    "data": [        
        {            
            "location": {                
                "type": "Point",                
                "coordinates": [                    
                    -83.338322,                    
                    42.705647                
                ],                
                "formattedAddress": "Some adress",                
                "city": "SomeCity",                
                "state": "STATE",                
                "zipcode": "000000",                
                "country": "US"            
            },            
            "_id": "630ce2c5a963f97ddae7387f",            
            "title": "Node Developer",            
            "description": "Must be a full-stack developer, able to implement everything in a MEAN or MERN stack paradigm (MongoDB, Express, Angular and/or React, and Node.js).",            
            "email": "employer@gmail.com",            
            "address": "someADress",            
            "company": "Company Ltd",            
            "industry": [                
                "Information Technology"            
            ],            
            "jobType": "Permanent",            
            "minEducation": "Bachelors",            
            "positions": 2,            
            "experience": "No Experience",            
            "salary": 155000,            
            "lastDate": "2022-09-05T15:41:44.912Z",            
            "user": "630cdf34b2bc6356a75ec29c",            
            "postingDate": "2022-08-29T16:01:09.707Z",            
            "slug": "node-developer"        
        }    
    ] 
}

我的C#请求代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class test : MonoBehaviour
{
    public string domain = "http://localhost:3000/api/v1";
    public string request = "/jobs";

    private bool coroutineRunning = false;

    Request req;

    private void Start()
    {
        req = new Request();
    }
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) && !coroutineRunning)
        {
            StartCoroutine(GetRequest(domain + request));
        }
    }

    IEnumerator GetRequest(string uri)
    {
        coroutineRunning = true;

        UnityWebRequest request = UnityWebRequest.Get(uri);

        yield return request.SendWebRequest();

        if (request.result == UnityWebRequest.Result.Success)
        {
            string jsonText = request.downloadHandler.text;

            Debug.Log(jsonText);

            SetUpObject(jsonText);
        }
        else
        {
            Debug.LogError(request.result);
        }

        coroutineRunning = false;
    }

    void SetUpObject(string json)
    {
        req = JsonUtility.FromJson<Request>(json);

        Debug.Log(JsonUtility.ToJson(req, true));

        Debug.Log(req);
    }
}

对应的序列化类代码

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[Serializable]
public class Data
{
    public Location location { get; set; }
    public string _id { get; set; }
    public string title { get; set; }
    public string description { get; set; }
    public string email { get; set; }
    public string address { get; set; }
    public string company { get; set; }
    public string[] industry { get; set; }
    public string jobType { get; set; }
    public string minEducation { get; set; }
    public int positions { get; set; }
    public string experience { get; set; }
    public int salary { get; set; }
    public DateTime lastDate { get; set; }
    public string user { get; set; }
    public DateTime postingDate { get; set; }
    public string slug { get; set; }
}

[Serializable]
public class Location
{
    public string type { get; set; }
    public double[] coordinates { get; set; }
    public string formattedAddress { get; set; }
    public string city { get; set; }
    public string state { get; set; }
    public string zipcode { get; set; }
    public string country { get; set; }
}
[Serializable]
public class Request
{
    public bool success { get; set; }
    public int results { get; set; }
    public Data[] data { get; set; }
}

问题说明

我用上述代码尝试序列化API返回的JSON,但无法得到正确的对象。我怀疑DateTime类型和JSON中的字符串格式不匹配可能是原因之一,但哪怕如此,Request类中的success和results字段至少应该能被正确填充才对,结果现在这些字段都没有正确值。我是通过JSON转C#工具生成的类,并且把生成类中的所有List<T>替换成了对应类型的数组,现在想知道如何创建能被Unity的JsonUtility正确序列化的类。


内容的提问来源于stack exchange,提问作者Yung_Paun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 13:42:22