简体   繁体   English

在C#中解析stdClass对象

[英]Parse stdClass object in C#

I'm currently working on a client that has more features than another existing client for a game. 我目前正在开发一个客户端,它具有比游戏的另一个现有客户端更多的功能。 One of the functionalities I would like to add is the ability for a user to add an IP address to their favourites and display relevant information of that server (IP, Port, Hostname, Gamemode etc.). 我想添加的功能之一是用户能够将IP地址添加到他们的收藏夹并显示该服务器的相关信息(IP,端口,主机名,游戏模式等)。

In order to do this I use an API which takes several parameters, such as the IP address and port. 为了做到这一点,我使用一个API,它接受几个参数,如IP地址和端口。 It returns some kind of string which contains the information that I need. 它返回某种包含我需要的信息的字符串。 However I'm having trouble extracting the information I want from the string. 但是我无法从字符串中提取我想要的信息。 I did manage to do it, but I did it quick and dirty by using a dozen substrings to cut out the values of the parameters from the response. 我确实设法做到了,但是我通过使用十几个子串来从响应中删除参数的值来快速而又脏。

Here's an example of an API call: http://monitor.sacnr.com/api/?IP=149.202.89.123&Port=7777&Action=info 以下是API调用的示例: http//monitor.sacnr.com/api/?IP = 149.202.89.123&Port = 7777&Action = info

Initially I thought the response was some kind of JSON string, but after some googling I found a question on Stackoverflow about stdClass. 最初我认为响应是某种JSON字符串,但经过一些谷歌搜索后,我在Stackoverflow上发现了一个关于stdClass的问题 Apparently it's some kind of generic PHP class. 显然它是某种通用的PHP类。

So my question is: how can I convert the response of the API to say JSON or XML, so I can easily parse it? 所以我的问题是:如何将API的响应转换为JSON或XML,以便我可以轻松解析它?

Example response: 响应示例:

O:8:"stdClass":18:{s:8:"ServerID";s:7:"1780392";s:2:"IP";s:14:"149.202.89.123";s:4:"Port";s:4:"7777";s:8:"Hostname";s:32:"Next Generation Roleplay (0.3.7)";s:8:"Gamemode";s:14:"NG:RP v3.0.409";s:8:"Language";s:7:"English";s:3:"Map";s:13:"NG-Gaming.net";s:10:"MaxPlayers";s:3:"500";s:7:"Players";s:3:"131";s:7:"Version";s:8:"0.3.7-R2";s:8:"Password";s:1:"0";s:4:"Time";s:5:"20:00";s:6:"WebURL";s:13:"ng-gaming.net";s:4:"Rank";s:3:"112";s:10:"AvgPlayers";s:5:"88.27";s:9:"HostedTab";s:1:"1";s:10:"LastUpdate";s:10:"1486138780";s:12:"TotalServers";s:4:"1305";}

Try using the Sharp Serialization Library . 尝试使用Sharp序列化库

As per their description: "Sharp Serialization Library serializes and deserializes primitives, ArrayLists and Hashtables, compatible with PHP serialize()" 根据他们的描述:“Sharp Serialization Library序列化和反序列化原语,ArrayLists和Hashtables,与PHP serialize()兼容”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM