Unity使用JsonUtility、ListJson、Newtonsoft解析JSON数据附带每种方式所踩过的坑!

使用JsonUtility(Unity自带)解析数据

json实体类

[Serializable]     //序列化
public class Root
{
    /// <summary>
    /// 是否还有下一页,返回值:true、false;如果不分页,不用关注该字段
    /// </summary>
    public bool has_more;
    /// <summary>
    ///  唯一的log id,用于问题定位
    /// </summary>
    public int log_id;
    /// <summary>
    /// 返回结果数量
    /// </summary>
    public int result_num;
    /// <summary>
    /// 结果对象
    /// </summary>
    public List<Result> result;
}

[Serializable]   //序列化
public class Result
{
    /// <summary>
    /// 返回的图片的介绍
    /// </summary>
    public string brief;
    /// <summary>
    /// 返回图片的相似程度
    /// </summary>
    public Double score;
    /// <summary>
    /// 图片签名,可以用来删除图片或定位问题
    /// </summary>
    public string cont_sign;
}

解析方法的代码:

//用于测试的json数据
 private static string json = "{\"has_more\": true, \"log_id\": 762076831, \"result_num\": 2, \"result\": [{\"brief\": \"窗帘打开\", \"score\": 1, \"cont_sign\": \"3462085514,3588976364\"}, {\"brief\": \"窗帘\", \"score\": 0.83868205547333, \"cont_sign\": \"2499474566,160859837\"}]}";
 
 #region 使用Unity自带的JsonUtility
    public static void ReturnJsonDataByJsonUtility()
    {
        //Root root = new Root();
        print("当前传入的json" + json);
        Root root = JsonUtility.FromJson<Root>(json);

        print("当前解析的数据:" + root.has_more);
        print("当前解析的数据:" + root.log_id);
        print("当前解析的数据:" + root.result[0].brief);
    }
    #endregion

结果:
在这里插入图片描述

注意事项(踩过的坑!!!):

  • 用于接收的JSON实体类需要声明[Serializable] 序列化。
  • 使用Unity自带方法时,实体类如果是属性成员(public bool has_more{get;set;})的话,在序列化的时候会缺失这些成员,导致解析不出来。将属性改为字段即可
  • 如果解析的对象是数组,自带的不能成功解析,可以人为将其封装为JSON对象。

JSON数组(自带的功能不能解析):

[{"brief": "窗帘打开", "score": 1, "cont_sign": "3462085514,3588976364"}, {"brief": "窗帘", "score": 0.83868205547333, "cont_sign": "2499474566,160859837"}]

改变为对象:

{"result": [{"brief": "窗帘打开", "score": 1, "cont_sign": "3462085514,3588976364"}, {"brief": "窗帘", "score": 0.83868205547333, "cont_sign": "2499474566,160859837"}]}

使用ListJson解析JSON数据

下载地址 提取码:qzw3
也是需要声明实体类进行接收,但是类可以不用声明[Serializable]
实体类跟上面的一样这里不再写出来

解析的方法:

 public static void ReturnJsonDataByLitJson()
    {
        Root root = JsonMapper.ToObject<Root>(json);
        print("当前解析的数据:" + root.has_more);
        print("当前解析的数据:" + root.log_id);
        print("当前解析的数据:" + root.result[0].brief);
    }

结果:
在这里插入图片描述
注意点:

  • 用于接收的实体类不需要声明[Serializable]
  • 用于接收的实例类可以是字段,也可以是属性,二者均可以成功解析。
  • 也需要声明用于接受JSON数据实体类。
  • 可以解析JSON对象,也可以解析JSON数组。

使用Newtonsoft解析数据

下载地址 提取码:082v

  #region 使用Newtonsoft解析JSON数据
    public static void ReturnJSONByNewtonsoft()
    {
        Root root = JsonConvert.DeserializeObject<Root>(json);
        print("当前解析的数据:" + root.has_more);
        print("当前解析的数据:" + root.log_id);
        print("当前解析的数据:" + root.result[0].brief);
    }
    #endregion

结果为:
在这里插入图片描述
如有哪里说的不对的地方,欢迎大家指点。

  • 11
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Unity中,使用Newtonsoft.Json解析JSON数据是一种常见的做法。你可以将Newtonsoft.Json.dll文件包含在你的项目中,然后使用它来对JSON数据进行解析。这个程序包不需要与jillejr.newtonsoft.json-for-unity软件包一起使用,因为它提供了自己的Newtonsoft.Json实现。你可以在Unity的生态系统中找到许多关于Newtonsoft.Json的分支和变体,但是你可以选择使用该程序包提供的版本。如果你需要使用Newtonsoft.Json使用教程,你可以参考CSDN上的一个文章,链接如下:。这个教程将向你展示如何在Unity使用Newtonsoft.Json解析JSON数据。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Newtonsoft.Json-for-Unity.Converters:Newtonsoft.Json的常见Unity类型的转换器。 与jilleJrNewtonsoft....](https://download.csdn.net/download/weixin_42112894/18775229)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [UnityNewtonsoft.Json](https://download.csdn.net/download/weixin_38239050/14803540)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [UnityIK人体骨骼反向动力学系统插件 Final IK 2.2](https://download.csdn.net/download/mayzhengxi/88262273)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值