繁体   English   中英

API 返回包含换行符等的 json。我该如何清理我的 json?

[英]API returns json containing linebreaks etc. how can i clean my json?

我正在尝试与 api (isbndb.com) 交谈,我可以成功地得到响应。 使用流阅读器我什至可以得到一个包含 json 的字符串。 但是,该字符串还将包含换行符等的特殊字符。因此我无法反序列化 json。 字符串内容如下所示:

"{\n    \"book\":\n                         \n                        {\n                                    \"publisher\":\"Pearson\",\n                                                                    \"language\":\"Eng\",\n                                                                                    \"image\":\"https:\\/\\/images.isbndb.com\\/covers\\/34\\/13\\/9780134093413.jpg\",\n                                                    \"title_long\":\"Campbell Biology (11th Edition)\",\n                                                    \"edition\":\"11\",\n                                                                    \"pages\":1488,\n                                                    \"date_published\":\"2017\",\n                                                                    \"subjects\":[\"Biology\"],\n                                                    \"authors\":[\"Lisa A. Urry\",\"Michael L. Cain\",\"Steven A. Wasserman\",\"Peter V. Minorsky\",\"Jane B. Reece\"],\n                                                    \"title\":\"Campbell Biology (11th Edition)\",\n                                                    \"isbn13\":\"9780134093413\",\n                                                    \"msrp\":\"259.99\",\n                                                    \"binding\":\"Hardcover\",\n                                                    \"publish_date\":\"2017\",\n                            \n                            \n                                    \"isbn\":\"0134093410\"\n                            }\n                    }"

让我得到这个结果的方法是这样的:

public bool TryGetBook(string isbn)
        {
            bool rtnValue = false;

            string WEBSERVICE_URL = Globals.WEBSERVICE_URL + isbn;

            try
            {
                var webRequest = WebRequest.Create(WEBSERVICE_URL);

                if (webRequest != null)
                {
                    webRequest.Method = "GET";
                    webRequest.ContentType = "application/json";
                    webRequest.Headers["Authorization"] = Globals.REST_KEY;

                    //Get the response 
                    WebResponse wr = webRequest.GetResponseAsync().Result;
                    Stream receiveStream = wr.GetResponseStream();
                    StreamReader reader = new StreamReader(receiveStream);

                    string content = reader.ReadToEnd();

                    if (content != null)
                    {
                        Book book = JsonConvert.DeserializeObject<Book>(content);

                        if (book != null)
                        {
                            Books.Add(book);
                            rtnValue = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return rtnValue;
        }

我不太确定在这里做什么,我可以编写一个正则表达式来清理我的样本数据,但这似乎是错误的方法。

任何帮助深表感谢。

您可以使用以下代码段替换所有换行符

string content = json.Replace("\n", "");

Environment.NewLine在这里不起作用。 下面有格式化的内容

{
    "book": 
    {
        "publisher": "Pearson",
        "language": "Eng",
        "image": "https:\/\/images.isbndb.com\/covers\/34\/13\/9780134093413.jpg",
        "title_long": "Campbell Biology (11th Edition)",
        "edition": "11",
        "pages": 1488,
        "date_published": "2017",
        "subjects": ["Biology"],
        "authors": ["Lisa A. Urry", "Michael L. Cain", "Steven A. Wasserman", "Peter V. Minorsky", "Jane B. Reece"],
        "title": "Campbell Biology (11th Edition)",
        "isbn13": "9780134093413",
        "msrp": "259.99",
        "binding": "Hardcover",
        "publish_date": "2017",
        "isbn": "0134093410"
    }
}

然后复制更新的字符串并使用Edit-Paste Special-Paste JSON as classes 有生成的类

public class BookResponse
{
    public Book book { get; set; }
}

public class Book
{
    public string publisher { get; set; }
    public string language { get; set; }
    public string image { get; set; }
    public string title_long { get; set; }
    public string edition { get; set; }
    public int pages { get; set; }
    public string date_published { get; set; }
    public string[] subjects { get; set; }
    public string[] authors { get; set; }
    public string title { get; set; }
    public string isbn13 { get; set; }
    public string msrp { get; set; }
    public string binding { get; set; }
    public string publish_date { get; set; }
    public string isbn { get; set; }
}

实际上, Book实例嵌套在不同的类中。 所以,为了解析你必须使用类似的东西

var response = JsonConvert.DeserializeObject<BookResponse>(content);

if (response.book != null)
{
}

您可以先尝试用空字符串替换换行符。 content = content.Replace("\\\\n",""); 然后反序列化。 祝你好运。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM