解析json响应为null

时间:2012-11-03 17:18:33

标签: c# json parsing

解析json时遇到问题。

json数据在这里:http://beta.fmeserver.com/fmerest/engines/.json?token=0ccfa0400b2d760fa3519baf18a557edb118356e

我使用json2csharp创建了一些类,但搜索为null:

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

1 个答案:

答案 0 :(得分:0)

如何使用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);
    }
}