zoukankan      html  css  js  c++  java
  • [Unity工具]json查看工具03:Newtonsoft.Json的使用

    参考链接:

    http://code.sike.wang/code/show-6217.html

    https://blog.csdn.net/admans/article/details/101361684

    Newtonsoft.Json官网:https://www.newtonsoft.com/json

    对应文档:https://www.newtonsoft.com/json/help/html/Introduction.htm

    1.Newtonsoft.Json.dll

    可以直接搜索去下载Newtonsoft.Json.dll,或者去github官网(https://github.com/JamesNK/Newtonsoft.Json)下载源码后生成dll,这里介绍一下后者:

    下载源码后,点菜单栏生成/生成解决方案,这时可能会报错(如果没则忽略):

    提示需要安装.NET Core SDK,下载地址在:https://dotnet.microsoft.com/download

    这里我下载的是.NET Core 3.0,安装好后,cmd输入dotnet可以查看是否安装成功

    安装好后再次生成,这时可能会提示vs版本不对(如果没则忽略),查了下发现vs2019版本16.3才正式支持.NET Core 3.0,因此点击菜单栏帮助/检查更新,去更新下vs的版本即可

    这时再次点生成就没问题了(可能会有小报错但不影响),在binDebug下可以看到各个net版本下的dll

    2.在unity中使用Newtonsoft.Json.dll

    上面说到会生成各个net版本下的dll,那用哪个版本的会比较合适呢?可以打开Project Settings来看下。因此我选了net45目录下的,直接拖到unity中就好了

    使用如下(这里我直接用官网的例子):

     1 using Newtonsoft.Json;
     2 using Newtonsoft.Json.Linq;
     3 using System;
     4 using UnityEngine;
     5 
     6 public class Product
     7 {
     8     public string Name;
     9     public DateTime Expiry;
    10     public string[] Sizes;
    11 }
    12 
    13 public class Movie
    14 {
    15     public string Name;
    16     public DateTime ReleaseDate;
    17     public string[] Genres;
    18 }
    19 
    20 public class TestJson : MonoBehaviour
    21 {
    22     void Start()
    23     {
    24         //1.Serialize JSON
    25         Product product = new Product();
    26         product.Name = "Apple";
    27         product.Expiry = new DateTime(2008, 12, 28);
    28         product.Sizes = new string[] { "Small" };
    29 
    30         string json = JsonConvert.SerializeObject(product);
    31         Debug.Log(json);
    32 
    33         //2.Deserialize JSON
    34         string json2 = @"{
    35           'Name': 'Bad Boys',
    36           'ReleaseDate': '1995-4-7T00:00:00',
    37           'Genres': [
    38             'Action',
    39             'Comedy'
    40           ]
    41         }";
    42 
    43         Movie m = JsonConvert.DeserializeObject<Movie>(json2);
    44         Debug.Log(m.Name);
    45 
    46         //3.LINQ to JSON
    47         JArray array = new JArray();
    48         array.Add("Manual text");
    49         array.Add(new DateTime(2000, 5, 23));
    50 
    51         JObject o = new JObject();
    52         o["MyArray"] = array;
    53 
    54         string json3 = o.ToString();
    55         Debug.Log(json3);
    56 
    57         foreach (var item in o)
    58         {
    59             Debug.LogWarning(item.Key);
    60             Debug.LogWarning(item.Value);
    61         }
    62 
    63         foreach (var item in array)
    64         {
    65             Debug.LogError(item.ToString());
    66         }
    67     }
    68 }

    输出如下:

  • 相关阅读:
    VirtualBox Win7 虚拟机 共享文件夹设置
    图像的 SNR 和 PSNR 的计算
    swprintf %s %ws %S 的区别
    解决 Virtual Box 启动 Cannot load R0 module supLoadModule returned VERR_LDR_MISMATCH_NATIVE Failed to register ourselves as a PCI Bus (VERR_MODULE_NOT_FOUND)
    VS2015/2013/2012 IIS Express Debug Classic ASP
    Beginning Android 4 中 Demo Basic/Switch 的问题.
    【虚拟机】解决网络适配器没有 VirtualBox Host-Only Ethernet Adapter 问题
    Docker安装Tomcat镜像并部署web项目
    Docker——入门实战
    Centos7上安装docker
  • 原文地址:https://www.cnblogs.com/lyh916/p/11756026.html
Copyright © 2011-2022 走看看