簡體   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