Unity C#Json读/写

时间:2019-05-08 20:27:12

标签: c# json read-write

我的代码引用了https://docs.unity3d.com/ScriptReference/JsonUtility.html

在我的LoadJson()方法内部是一条注释掉的打印语句,用于显示我知道的某个json值

x["Character0"]["Head"].ToString()

要显示总价值,我想我的问题是方法中的返回类型

 public static SaveCharacters CreateFromJSON(string jsonString)

但是我对C#中的Json茫然无措,我没有找到任何有用的解决方案,这是很好的帮助。

Json文件

{
"Character0":{
    "Head":"HeadCube",
    "Neck":"NeckCube",
    "Body":"BodyCube",
    "Arms":"Sphere_Capsule_Arms",
    "Legs":"Sphere_Capsule_Legs"
    }
}

代码

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

[System.Serializable]
public class SaveCharacters
{
private string json;
private string Head, Neck, Body, Arms, Legs;

private static SaveCharacters _Instance;

public static SaveCharacters Instance { get { return _Instance; } }
void Awake() {
    if (_Instance != null)
    {
        _Instance = this;
    }
}
// Start is called before the first frame update
public void starts()
{
    LoadJson();
}

private void LoadJson()
{
    using (StreamReader r = new StreamReader("Assets/JSON/CustomCharacters.json"))
    {
        json = r.ReadToEnd();
        Debug.Log(json);
        SaveCharacters x =CreateFromJSON(json);

        //Debug.Log(x[""][""]);
    }
}
public void Load(string savedData)
{
    JsonUtility.FromJsonOverwrite(savedData, this);
}
public string SaveToString()
{
    return JsonUtility.ToJson(this);
}
public static SaveCharacters CreateFromJSON(string jsonString)
{
    return JsonUtility.FromJson<SaveCharacters>(jsonString);
}
}

1 个答案:

答案 0 :(得分:1)

让我们假设您拥有此Json文件:

def on_batch_begin(self, batch, logs=None):
    actual_lr = float(K.get_value(self.model.optimizer.lr))
    decayed_learning_rate = actual_lr * self.decay_rate ^ (self.global_step / self.decay_steps)
    K.set_value(self.model.optimizer.lr, decayed_learning_rate)
    self.global_step += 1

您想获取分数值:

首先:下载此库SimpleJson

然后这样称呼它:

on_epoch_begin(self, epoch, logs=None)