【问题标题】:How to edit JSON API response如何编辑 JSON API 响应
【发布时间】:2016-02-19 06:19:56
【问题描述】:

我正在使用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;
        }
    }

【问题讨论】:

  • 您正在从文件中读取数据并作为字符串发送回,有 2 个选项,创建一个代表 { "name": "apple.com", "status": "regthroughothers", "classkey": "domcno" } 的类,并在读取后创建一个对象列表并发送该列表,或更简单的解决方案是将文件中的数据更改为表示格式
  • 如果你不介意你能写完整的代码吗...我们该怎么办??因为我正在尝试这个,但我无法编写确切的代码。

标签: c# json api json.net


【解决方案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】:

    我在使用您的 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 格式转换成另一种格式的意义,所以我把它留给你。

    【讨论】:

      【解决方案3】:

      除非您可以控制服务器,或者服务器为此公开 API,否则您无法更改服务器返回的响应。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-04
        • 2020-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-30
        • 2019-06-23
        • 1970-01-01
        相关资源
        最近更新 更多