• 大小: 5.12MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-11-21
  • 语言: 其他
  • 标签: Unity2018  Json读写  

资源简介

unity2018的 Json 文件的序列化反序列化读写工程,主要是支持反序列化读取Json文件,序列化写入Json文件的unity2018的Demo。里面自带了Newtonsoft.Json的Newtonsoft.Json.dll。

资源截图

代码片段和文件信息

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using System;
using System.Text;
using System.IO;

public class CSerializer : MonoBehaviour
{
    
    private void Start()
    {
        string path = Application.dataPath + “/test.json“;
        Product products = new Product();
        products.dic[“字典key“] = “字典Value“;
        products.name = “字符test“;
        string sjson = JsonConvert.Serializeobject(products);
        File.WriteAllText(path sjson Encoding.UTF8);

        Product mProduct = JsonConvert.Deserializeobject(sjson);

        Debug.Log(sjson);
        Debug.Log(mProduct.name);


        path = Application.dataPath + “/inputdate.json“;
        //序列换格式1
        JsonData jsonData = new JsonData();
        jsonData.Int = 1;
        jsonData.String = “123“;
        jsonData.Float = 1.33f;
        jsonData.Double = 3.2;
        //转Json
        string jsons = JsonConvert.Serializeobject(jsonData);
        //File.WriteAllText(path jsons Encoding.UTF8);
        Debug.Log(“序列换格式1“+jsons);


        //序列换格式2
        Product product = new Product();
        product.dic[“键“] = “值“;
        product.name = “字符“;
        string json = JsonConvert.Serializeobject(product);
        File.WriteAllText(path json Encoding.UTF8);
        Debug.Log(“序列换格式2“ + json);

        //获取文件路径。

        if (File.Exists(path))                          //如果该文件存在。
        {
            string dataAsJson = File.ReadAllText(path); //读取所有数据送到json格式的字符串里面。

            //直接赋值。FromJson
           
            Product m = JsonConvert.Deserializeobject(dataAsJson);
            

            //使用已有对象,添加值。FromJsonOverwrite
            //loadedData = new MyData();
            //JsonUtility.FromJsonOverwrite(dataAsJson loadedData);

            Debug.Log(“---“+m.name);
        }
    }
}


[Serializable]
public class CharacterListItem
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Level { get; set; }
    public string Class { get; set; }
    public string Sex { get; set; }
}

public class JsonData
{
    public int Int;
    public float Float;
    public string String;
    public double Double;
}

public class Product
{
    public string name;
    public Dictionary dic = new Dictionary();
}


 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2019-07-23 02:37  Json_Serializer_ReadAndWrite\
     目录           0  2019-07-23 01:43  Json_Serializer_ReadAndWrite\.vs\
     目录           0  2019-07-23 01:43  Json_Serializer_ReadAndWrite\.vs\config\
     文件       82755  2018-06-26 21:12  Json_Serializer_ReadAndWrite\.vs\config\applicationhost.config
     目录           0  2019-07-23 01:16  Json_Serializer_ReadAndWrite\.vs\Json_Serializer_ReadAndWrite\
     目录           0  2019-07-23 01:16  Json_Serializer_ReadAndWrite\.vs\Json_Serializer_ReadAndWrite\DesignTimeBuild\
     文件         168  2019-07-23 02:37  Json_Serializer_ReadAndWrite\.vs\Json_Serializer_ReadAndWrite\DesignTimeBuild\.dtbcache
     目录           0  2019-07-23 01:02  Json_Serializer_ReadAndWrite\.vs\Json_Serializer_ReadAndWrite\v15\
     文件      242176  2019-07-23 02:37  Json_Serializer_ReadAndWrite\.vs\Json_Serializer_ReadAndWrite\v15\.suo
     目录           0  2019-07-23 01:00  Json_Serializer_ReadAndWrite\.vs\Json_Serializer_ReadAndWrite\v15\Server\
     目录           0  2019-07-23 01:00  Json_Serializer_ReadAndWrite\.vs\Json_Serializer_ReadAndWrite\v15\Server\sqlite3\
     文件           0  2019-07-23 01:00  Json_Serializer_ReadAndWrite\.vs\Json_Serializer_ReadAndWrite\v15\Server\sqlite3\db.lock
     文件      958464  2019-07-23 01:51  Json_Serializer_ReadAndWrite\.vs\Json_Serializer_ReadAndWrite\v15\Server\sqlite3\storage.ide
     文件       32768  2019-07-23 02:36  Json_Serializer_ReadAndWrite\.vs\Json_Serializer_ReadAndWrite\v15\Server\sqlite3\storage.ide-shm
     文件     4136512  2019-07-23 02:37  Json_Serializer_ReadAndWrite\.vs\Json_Serializer_ReadAndWrite\v15\Server\sqlite3\storage.ide-wal
     文件       41959  2019-07-23 02:37  Json_Serializer_ReadAndWrite\Assembly-CSharp.csproj
     目录           0  2019-07-23 02:36  Json_Serializer_ReadAndWrite\Assets\
     文件          40  2019-07-23 02:37  Json_Serializer_ReadAndWrite\Assets\inputdate.json
     文件         158  2019-07-23 01:38  Json_Serializer_ReadAndWrite\Assets\inputdate.json.meta
     文件      552872  2018-11-27 18:07  Json_Serializer_ReadAndWrite\Assets\Newtonsoft.Json.dll
     文件         569  2019-07-23 01:02  Json_Serializer_ReadAndWrite\Assets\Newtonsoft.Json.dll.meta
     目录           0  2019-07-23 02:33  Json_Serializer_ReadAndWrite\Assets\Scenes\
     文件         180  2018-05-09 22:30  Json_Serializer_ReadAndWrite\Assets\Scenes.meta
     文件        7649  2019-07-23 02:33  Json_Serializer_ReadAndWrite\Assets\Scenes\SampleScene.unity
     文件         162  2018-05-09 22:30  Json_Serializer_ReadAndWrite\Assets\Scenes\SampleScene.unity.meta
     目录           0  2019-07-23 02:37  Json_Serializer_ReadAndWrite\Assets\script\
     文件         172  2019-07-23 00:59  Json_Serializer_ReadAndWrite\Assets\script.meta
     文件        2598  2019-07-23 02:37  Json_Serializer_ReadAndWrite\Assets\script\CSerializer.cs
     文件         243  2019-07-23 00:59  Json_Serializer_ReadAndWrite\Assets\script\CSerializer.cs.meta
     文件          58  2019-07-23 02:37  Json_Serializer_ReadAndWrite\Assets\test.json
     文件         158  2019-07-23 01:44  Json_Serializer_ReadAndWrite\Assets\test.json.meta
............此处省略1154个文件信息

评论

共有 条评论