2

我在解析 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; }
    }
4

1 回答 1

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);
    }
}
于 2012-11-03T17:29:44.387 回答