如何编辑JSON API响应

时间:2016-02-19 06:19:56

标签: c# json api json.net

我正在使用this api

https://test.httpapi.com/api/domains/available.json?auth-userid=0&api-key=key&domain-name=domain1&domain-name=domain2&tlds=com&tlds=net

当我在这个API中传递一些参数时,它会以这种格式输出

{
"apple.com":{"status":"regthroughothers","classkey":"domcno"},
"asdfgqwx.com":{"status":"available","classkey":"domcno"},
"microsoft.org":{"status":"unknown"},
"apple.org":{"status":"unknown"},
"microsoft.com":{"status":"regthroughothers","classkey":"domcno"},
"asdfgqwx.org":{"status":"unknown"}
}

这个Json格式对我来说很奇怪..所以我想改变像这样的Json格式

[{
        "name": "apple.com",
        "status": "regthroughothers",
        "classkey": "domcno"
    },
    {
        "name": "asdfgqwx.com",
        "status": "available",
        "classkey": "domcno"
    },
    {
        "name": "microsoft.org",
        "status": "unknown",
        "classkey": ""
    },
    {
        "name": "apple.org",
        "status": "unknown",
        "classkey": ""
    },
    {
        "name": "microsoft.com",
        "status": "regthroughothers",
        "classkey": "domcno"
    },
    {
        "name": "asdfgqwx.org",
        "status": "unknown",
        "classkey": "domcno"
    }]

我正在使用此代码来使用API​​。

 protected string GET(string url)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        try
        {
            WebResponse response = request.GetResponse();
            using (Stream responseStream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.UTF8);
                return reader.ReadToEnd();
            }
        }
        catch (WebException ex)
        {
            WebResponse errorResponse = ex.Response;
            using (Stream responseStream = errorResponse.GetResponseStream())
            {
                StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.GetEncoding("utf-8"));
                String errorText = reader.ReadToEnd();
                // log errorText
            }
            throw;
        }
    }

3 个答案:

答案 0 :(得分:1)

我无法使用您的GET方法连接到api。它总是返回ConnectFailure ......

无论如何,api实际上是返回字典类型的json。因此,您必须创建一个类并相应地反序列化json响应。

    public class UrlObject
    {
        public string status { get; set; }
        public string classkey { get; set; }
    }

//string jsonResponse = GET("https://test.httpapi.com/api/domains/available.json?auth-userid=0&api-key=key&domain-name=domain1&domain-name=domain2&tlds=com&tlds=net");
string jsonResponse = "{\"apple.com\":{\"status\":\"regthroughothers\",\"classkey\":\"domcno\"},\"asdfgqwx.com\":{\"status\":\"available\",\"classkey\":\"domcno\"},\"microsoft.org\":{\"status\":\"unknown\"},\"apple.org\":{\"status\":\"unknown\"},\"microsoft.com\":{\"status\":\"regthroughothers\",\"classkey\":\"domcno\"},\"asdfgqwx.org\":{\"status\":\"unknown\"}}";

Dictionary<string, UrlObject> urlDictionary = JsonConvert.DeserializeObject<Dictionary<string, UrlObject>>(jsonResponse);

请注意,json反序列化成功,现在您可以访问这些值。由于我没有看到将json格式转换为另一种格式的意义,我将把它留给你。

答案 1 :(得分:1)

您可以使用Json.Net的LINQ-to-JSON API将JSON从一种格式转换为另一种格式,如果这是您真正想要做的事情。这是一种方法:

public static string TransformJson(string originalJson)
{
    return new JArray(
        JObject.Parse(originalJson).Properties().Select(jp => 
        {
            var jo = new JObject((JObject)jp.Value);
            jo.AddFirst(new JProperty("name", jp.Name));
            return jo;
        })
    ).ToString();
}

小提琴:https://dotnetfiddle.net/BoU8W5

答案 2 :(得分:0)

除非您可以控制服务器,否则服务器会公开API来执行此操作,否则您无法更改服务器返回的响应。

如果你想要一个不同的对象模型,例如如问题中所述,对象数组而不是对象字典,那么您需要编写一些代码以从API模型映射到您希望在应用程序/库中实际使用的模型。