解析JSON作为回应

时间:2014-10-29 07:16:59

标签: c# json

您好我收到了来自网络服务的回复,我希望从回复中获得一个网址。 我的回答是以下格式。

[{"cdn_streaming_uri": "9e849cfbb2e157-22558a0600b387d0abe240fe5.r73.stream..rackcdn.com", "name": "test1", "cdn_ios_uri": "d3d4c27-22558a0600b387d0abc071d0ae240kcdn.com", "cdn_ssl_uri": "https://990fea26e-22558a0600b387d0abc071d0ae240fe5.ssl.cdn.com", "cdn_enabled": false, "ttl": 259200, "log_retention": false, "cdn_uri": "99b56a009-22558a0600b3c071d0ae240fe5.r73.n.com"}, {"cdn_streaming_uri": "74ec8c-d5edc6cad91792413b1b134fde.r46.stcdn.com", "name": "test2", "cdn_ios_uri": "d05437e44-d5edc61792413b1b134fde.iosr.cdn.com", "cdn_ssl_uri": "https://a1c2ebbf5-d5edc6cd91792413b1b134fde.scdn.com", "cdn_enabled": false, "ttl": 259200, "log_retention": false, "cdn_uri": "72ffd-d5edc6ca16852413b1b134fde.cdn.com"}, {"cdn_streaming_uri": "93665b76-550971032c2a22cdn.com", "name": "test3", "cdn_ios_uri": "ca6b-550971032c2fbf19452d6a.iosr.cf2.rackcdn.com", "cdn_ssl_uri": "https://c7c39-550971032cbf19452d6cdn.com", "cdn_enabled": true, "ttl": 86400, "log_retention": true, "cdn_uri": "68fc6d831a94-550971032c252d6a.r3cdn.com"}]

我需要名为“test3”的“cdn_streaming_uri”。

您可以在http://json.parser.online.fr/中查看JSON解析器 我该如何解析它?

这是我的代码:

public static object getTokenResponse(String PrivateURL, string ResponseType)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(PrivateURL +"?format=JSON");
    request.Method = "GET";
    request.Headers.Add("X-Auth-Token", id);
    //request.ContentType = "application/XML";

    HttpWebResponse resp1;
    try
    {
        resp1 = (HttpWebResponse)request.GetResponse();
    }
    catch (Exception exp)
    {
        string[] st = new string[0];
        return st;
    }


    StreamReader reader = new StreamReader(resp1.GetResponseStream());
    string  secondresponse = reader.ReadToEnd();
    Console.WriteLine(secondresponse);
    reader.Close();

    JavaScriptSerializer json_serializer = new JavaScriptSerializer();
    object obj1 = json_serializer.DeserializeObject(secondresponse);
}

我可以在obj1中看到响应。

2 个答案:

答案 0 :(得分:9)

我认为,最好的方法是创建代表您的回答的课程。最简单的方法是使用Visual Studio的EDIT -> Paste Special -> Paste JSON As Classes选项:

enter image description here

您只需复制您的回复并将其粘贴为JSON类。 Visual Studio将为您生成模型。在这种特殊情况下,结果将是:

namespace ConsoleApplication91
{
    public class Rootobject
    {
        public Class1[] Property1 { get; set; }
    }

    public class Class1
    {
        public string cdn_streaming_uri { get; set; }
        public string name { get; set; }
        public string cdn_ios_uri { get; set; }
        public string cdn_ssl_uri { get; set; }
        public bool cdn_enabled { get; set; }
        public int ttl { get; set; }
        public bool log_retention { get; set; }
        public string cdn_uri { get; set; }
    }
}
当然,这看起来并不是很好,但是您总是欢迎重构此代码。当您拥有模型时,您可以下载响应,解析它并使用linq获取所需内容,例如:

using (var client = new WebClient())
{
    var url = "your service url";
    var serializer = new JavaScriptSerializer();

    // Response in JSON format
    var respJson = client.DownloadString(url);

    // Deserialized response
    var resp = serializer.Deserialize<Rootobject>(respJson);

    // Your requested result
    var result = resp.Property1.FirstOrDefault(o => o.name == "test3").cdn_streaming_uri;
}

<强>编辑:

重构后(使用DataMember属性并删除冗余模型对象),您可以使用以下模型:

[DataContract]
public class Model
{
    [DataMember(Name = "cdn_streaming_uri")]
    public string CdnStreamingUri { get; set; }

    [DataMember(Name = "name")]
    public string Name { get; set; }

    [DataMember(Name = "cdn_ios_uri")]
    public string CdnIosUri { get; set; }

    [DataMember(Name = "cdn_ssl_uri")]
    public string CdnSslUri { get; set; }

    [DataMember(Name = "cdn_enabled")]
    public bool CdnEnabled { get; set; }

    [DataMember(Name = "ttl")]
    public int Ttl { get; set; }

    [DataMember(Name = "log_retention")]
    public bool LogRetention { get; set; }

    [DataMember(Name = "cdn_uri")]
    public string CdnUri { get; set; }
}

经过一些研究后,我发现JavascriptSerializer有点弃用,并且它不支持任何类型的DataMemberAttributes。所以我建议使用DataContractJsonSerializer。它比JavascriptSerializer更乱,但我觉得它很好。如果您不关心任何代码约定,您可以安静地使用提供的第一个选项(JavaScriptSerializerDataMember属性)。并且不要忘记更新结果查询:

using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(respJson)))
{
    var serializer = new DataContractJsonSerializer(typeof(Model[]));
    var resp = serializer.ReadObject(ms) as Model[];

    var result = resp.FirstOrDefault(o => o.Name == "test3").CdnStreamingUri;
}

但是,如果您不想使用linq(我仍然强烈推荐),您可以创建一些功能,找到您需要的CdnStreamingUri

public static class Extensions
{
    public static string GetCdnStreamingUriFor(this Model[] input, string name)
    {
        foreach (var model in input)
        {
            if (model.Name == name)
                return model.CdnStreamingUri;
        }

        return string.Empty;
    }
}

您的结果查询将如下所示:

var result = resp.GetCdnStreamingUriFor("test3");

<强> P.S。 所有使用的命名空间的完整列表:

using System;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Runtime.Serialization;

答案 1 :(得分:1)

我建议您为该json生成类,如下所示(http://json2csharp.com/):

public class RootObject
{
    public string cdn_streaming_uri { get; set; }
    public string name { get; set; }
    public string cdn_ios_uri { get; set; }
    public string cdn_ssl_uri { get; set; }
    public bool cdn_enabled { get; set; }
    public int ttl { get; set; }
    public bool log_retention { get; set; }
    public string cdn_uri { get; set; }
}

之后,您可以反序列化强类型对象http://msdn.microsoft.com/en-us/library/bb355316(v=vs.110).aspx