JSON存储

文章讲述了如何在Unity3D中使用JSON格式进行数据操作,包括创建和验证JSON对象,以及使用LitJson库进行序列化和反序列化。内容涉及用户注册和登录的场景,强调了JSON文件的格式要求和数据一致性检查。
摘要由CSDN通过智能技术生成

1、JSON的{}大括号里只有一个对象

{"key":"value"}大括号里表示这是一个JSON对象,通过键和值来存储对象,key和value键必须用双引号来包裹

2、JSON文件里可以有多个对象

[{"Account":"1","Password":"1"},{"Account":"456","Password":"456"}],文件里可以创建多个对象

JSON格式一个标点符号都不能错,可以去这个网站来校检在线JSON校验格式化工具(Be JSON)


using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using LitJson;//注意,必须引入LitJson插件,否则引入不了这个命名空间

public class UI : MonoBehaviour
{
    public InputField D_Account, D_Password;
    public InputField Z_Account, Z_Password, Z_Password1;

    public Button D_Regiester, Lon;

    public Button Z_Register, Return;

    public RectTransform Pan1, Pan2;

    void Start()
    {

       string path = Application.dataPath + "/JSON/Person.json";//JSON文件资源路径;
        //File.Create(path).Dispose();//创建完JSON文件自动释放文件流所占用的资源;

        //注意:创建完JSON文件里必须初始化数据,否则运行会报空

       [{"Account":"1","Password":"1"},{"Account":"456","Password":"456"}]
        D_Regiester.onClick.AddListener(D_Regiesters);
        Return.onClick.AddListener(Returns);
        Z_Register.onClick.AddListener(Z_Registers);
        Lon.onClick.AddListener(Lons);
    }

    private void Lons()
    {
        Read();
    }

    private void Z_Registers()
    {
        Write();
    }

    private void Returns()
    {
        Pan1.gameObject.SetActive(true);
        Pan2.gameObject.SetActive(false);
        Z_Account.text = null;
        Z_Password.text = null;
        Z_Password1.text = null;
    }

    private void D_Regiesters()
    {
        Pan1.gameObject.SetActive(false);
        Pan2.gameObject.SetActive(true);
        D_Account.text = null;
        D_Password.text = null;
    }

    void Write()
    {

        if (Z_Password.text == Z_Password1.text)
        {
            bool f = true;
            string path = Application.dataPath + "/JSON/Person.json";
            string str = File.ReadAllText(path);//读出序列化后的文件
            List<JSON> jsons = JsonMapper.ToObject<List<JSON>>(str);//反序列化
            foreach (JSON js in jsons)
            {

                if (js.Account.Equals(Z_Account.text))
                {
                    f = false;
                    print("账号重复");
                    break;
                }
            }
            if (f)
            {
                print("注册成功");
                JSON jSON = new JSON();//创建新用户
                jSON.Account = Z_Account.text;
                jSON.Password = Z_Password.text;
                jsons.Add(jSON);//将新创建的用户添加至上面反序列化后的用户集合中
                string s = JsonMapper.ToJson(jsons);//将添加后的用户集合序列化并写入
                File.WriteAllText(path, s);
            }
        }
        else
        {
            print("密码不一致");
        }
    }
    void Read()
    {
        string path = Application.dataPath + "/JSON/Person.json";
        string str = File.ReadAllText(path);
        List<JSON> jsons = JsonMapper.ToObject<List<JSON>>(str);
        for (int i = 0; i < jsons.Count; i++)//如果用foreach遍历的话会遍历JSON里面所有的数据,会把所有的结果输出出来
        {
            if (jsons[i].Account.Equals(D_Account.text) && jsons[i].Password.Equals(D_Password.text))
            {
                print("登录成功");
                break;
            }
            else if (jsons.Count == i + 1)
            {
                print("登录失败");
            }
        }
    }
    public class JSON//封装类
    {
        public string Account { get; set; }

        public string Password { get; set; }
    }
}

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值