使用Newtonsoft.Json从. json文件中添加保存和加载价格(值)



我目前正在编辑和设置我自己使用的SteamBot。下面是它的源代码:https://github.com/Jessecar96/SteamBot
我已经为它做了自己的UserHandle,但我希望它从settings_prices加载(并保存)项目价格。json文件(现在它只记得价格,直到我重新启动。exe文件)。

static int SellPricePerKey = 70; // cena klucza w scrapach np. 31 / 9 = 3.55 ref
static int BuyPricePerKey = 69; // cena klucza w scrapach np e.g. 29 / 9 = 3.33 ref
static int SellPricePerTod = 33; // cena ToD'a w scrapach np. 31 / 9 = 3.55 ref
static int BuyPricePerTod = 28; // cena ToD'a w scrapach np. 29 / 9 = 3.33 ref

和价格变更命令(通过蒸汽聊天与bot),例如出售钥匙(游戏内物品)

else if (message.StartsWith("!sell key"))
{
// Usage: !sell newprice "e.g. sell 26"
    int NewSellPrice = 0;
    if (message.Length >= 10)
    {
        Bot.SteamFriends.SendChatMessage(OtherSID, type, "Aktualna cena sprzedazy kluczy to: " + 
            SellPricePerKeyInRefs + " ref (" 
                + SellPricePerKey + " scapow).");
        int.TryParse(message.Substring(9), out NewSellPrice);
        Bot.log.Success("Admin zmienil cene sprzedazy kluczy z " + 
            SellPricePerKeyInRefs + " ref, na " + 
            Math.Truncate(100 * (NewSellPrice / 9.0)) / 100 + 
            " ref (" + NewSellPrice + " scapow).");
        SellPricePerKey = NewSellPrice;
        double NewSellPricePerKeyInRefs = Math.Truncate(100 * (SellPricePerKey / 9.0)) / 100;
        SellPricePerKeyInRefs = NewSellPricePerKeyInRefs;
        Bot.SteamFriends.SendChatMessage(OtherSID, type, "Zmiana ceny sprzedazy kluczy na: " + 
            SellPricePerKeyInRefs + " ref (" + 
            SellPricePerKey + " scapow).");
        Bot.log.Success("Pomyslnie zmieniono cene sprzedazy kluczy.");
    }
    else
    {
        Bot.SteamFriends.SendChatMessage(OtherSID, type, 
            "Potrzebuje nowej ceny w komendzie. Aktualna cena sprzedazy to: " + 
            SellPricePerKeyInRefs + " refa (" + SellPricePerKey + " scapow).");
    }
}

但是我希望它保存和加载这些价格从settings_prices。Json文件,看起来像这样:

{
"KeySell": "70"
"KeyBuy": "69"
"TodSell": "33"
"TodBuy": "28"
}

应该这样做:

void Main()
{
    var my_object = new MyObject() {
          SellPricePerKey = 70, // cena klucza w scrapach np. 31 / 9 = 3.55 ref
          BuyPricePerKey = 69, // cena klucza w scrapach np e.g. 29 / 9 = 3.33 ref
          SellPricePerTod = 33,// cena ToD'a w scrapach np. 31 / 9 = 3.55 ref
          BuyPricePerTod = 28 // c
    };
    // create the JSON
    var json_my_object = Newtonsoft.Json.JsonConvert.SerializeObject(my_object);
    // Retrun an a JObject with 4 objects
    var normal_deserialized_object = Newtonsoft.Json.JsonConvert.DeserializeObject(json_my_object);
    // Or deserialize object and play with inner bits
    Newtonsoft.Json.Linq.JObject another_deserialized_object =
        (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject(json_my_object);
    foreach (var pair   in another_deserialized_object)
    {
        Console.WriteLine("Key:[{0}] , Value:[{1}]", pair.Key, pair.Value);
    }
}
// Define other methods and classes here
public class MyObject {
    public  int SellPricePerKey ;
    public  int BuyPricePerKey;
    public  int SellPricePerTod ;
    public  int BuyPricePerTod;
}

它会返回:

{"SellPricePerKey":70,"BuyPricePerKey":69,"SellPricePerTod":33,"BuyPricePerTod":28}

要保存,将该JSON转储到文件中。要加载,从文件中读取成字符串,并将该字符串传递给JSON反序列化,然后您将拥有您的对象。

相关内容

  • 没有找到相关文章

最新更新