0

我目前正在编辑和设置 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

和价格更改命令(通过与机器人的蒸汽聊天),例如 Selling Key(游戏内物品)

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

1 回答 1

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反序列化,你就会得到你的对象。

于 2014-05-04T00:01:36.977 回答