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

时间:2014-05-03 23:47:45

标签: c# json json.net steambot

我正在编辑和设置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的steamchat)例如卖钥匙(游戏内物品)

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"
}

1 个答案:

答案 0 :(得分:0)

这应该做:

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反序列化,然后您就可以拥有自己的对象。