我在解析 json 时遇到问题。
json 数据在这里:http://beta.fmeserver.com/fmerest/engines/.json?token=0ccfa0400b2d760fa3519baf18a557edb118356e .
我用 json2csharp 创建了一些类,但搜索为空:
var url = "http://beta.fmeserver.com/fmerest/engines/.json?token=0ccfa0400b2d760fa3519baf18a557edb118356e";
WebClient client = new WebClient();
var json = client.DownloadString(url);
var search = JsonConvert.DeserializeObject<ServiceResponse>(json);
public class Engine
{
public int FMEBuildNumber { get; set; }
public string FMEHostName { get; set; }
public string FMEInstanceName { get; set; }
public int currentJobID { get; set; }
public int maxTransactionResultFailure { get; set; }
public int maxTransactionResultSuccess { get; set; }
public int resultFailureCount { get; set; }
public int resultSuccessCount { get; set; }
public int transactionPort { get; set; }
}
public class Engines
{
public List<Engine> engine { get; set; }
}
public class ServiceResponse
{
public string requestURI { get; set; }
public string token { get; set; }
public Engines engines { get; set; }
}
[JsonObject(MemberSerialization.OptIn)]
public class RootObject
{
[JsonProperty("serviceResponse")]
public ServiceResponse ServiceResponse { get; set; }
}
最佳答案
使用 Json.Net 以动态方式怎么样? ? (不使用 http://json2csharp.com/ 生成的任何类)
var url = "http://beta.fmeserver.com/fmerest/engines/.json?token=0ccfa0400b2d760fa3519baf18a557edb118356e";
using (WebClient wc = new WebClient())
{
string json = wc.DownloadString(url);
dynamic dynobj = JsonConvert.DeserializeObject(json);
foreach (var engine in dynobj.serviceResponse.engines.engine)
{
Console.WriteLine("{0} {1}", engine.FMEInstanceName, engine.transactionPort);
}
}
关于c# - 解析 json 响应为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13211554/