【问题标题】:How to convert a JArray into a string?如何将 JArray 转换为字符串?
【发布时间】:2014-04-25 12:19:17
【问题描述】:

我正在制作一个 Steam 交易机器人,我希望能够获取我的库存并将其发送给其他用户,这就是我所做的(使用 SteamKit2):

dynamic Response;
string Inv;
string name = Bot.SteamFriends.GetFriendPersonaName(OtherSID);
System.Console.WriteLine(name + " Said: " + message);
Response = Inventory.GetInventory(Bot.SteamClient.SteamID);
System.Console.WriteLine(Inv = Response.rgInventory);
Bot.SteamFriends.SendChatMessage(OtherSID, EChatEntryType.ChatMsg, Inv);

我收到以下错误:

[TF2 Bot 2014-04-25 20:13:18] ERROR: Microsoft.CSharp.RuntimeBinder.RuntimeBinde
rException: Cannot implicitly convert type 'Newtonsoft.Json.Linq.JArray' to 'str
ing'. An explicit conversion exists (are you missing a cast?)
at CallSite.Target(Closure , CallSite , Object )
at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T
0 arg0)
at SteamBot.SimpleUserHandler.OnMessage(String message, EChatEntryType type)
in C:\Users\Raka Gunarto\Documents\GitHub\SteamBot\SteamBot\UserHandler.cs:line
47
at SteamBot.Bot.<HandleSteamMessage>b__8(FriendMsgCallback callback) in C:\Us
ers\Raka Gunarto\Documents\GitHub\SteamBot\SteamBot\Bot.cs:line 481
at SteamKit2.CallbackMsg.Handle[T](Action`1 handler)
at SteamBot.Bot.HandleSteamMessage(CallbackMsg msg) in C:\Users\Raka Gunarto\
Documents\GitHub\SteamBot\SteamBot\Bot.cs:line 471
at SteamBot.Bot.BackgroundWorkerOnDoWork(Object sender, DoWorkEventArgs doWor
kEventArgs) in C:\Users\Raka Gunarto\Documents\GitHub\SteamBot\SteamBot\Bot.cs:l
ine 731

所以我的问题是:如何将 JArray 转换为 String? 如果只是在 System.Console.WriteLine 中做 Response.rgInventory 就可以了。但是当我尝试向某人发送消息时,这并不好,因为 SendChatMessage 函数接受字符串作为输入。

【问题讨论】:

  • 旧的 toString() 方法对你有用吗?

标签: c# string bots steam


【解决方案1】:

根据实现,您应该可以使用:

Inv = Response.rgInventory.toString();

如果这不能产生令人满意的结果,您可能必须自己构造字符串。您始终可以继续并覆盖 toString() 方法以使其符合您的需求。

【讨论】:

  • 谢谢你的快速回复,我会试试的。
  • 它说 JArray 不包含“toString”的定义
猜你喜欢
  • 2016-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-23
相关资源
最近更新 更多