API returns 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;
}
我不太确定在这里做什么,我可以写一个正则表达式来清理我的样本数据,但这似乎是错误的方法。
非常感谢任何帮助。
您可以先尝试用空字符串替换换行符。
content = content.Replace("\n","");
然后反序列化。祝你好运。
您可以使用以下代码段替换所有换行符
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"
}
}
然后复制更新后的字符串并使用编辑-特殊粘贴-粘贴JSON as classes。生成了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
实例嵌套在另一个 class 中。所以,为了解析你必须使用类似的东西
var response = JsonConvert.DeserializeObject<BookResponse>(content);
if (response.book != null)
{
}
我正在尝试与 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;
}
我不太确定在这里做什么,我可以写一个正则表达式来清理我的样本数据,但这似乎是错误的方法。
非常感谢任何帮助。
您可以先尝试用空字符串替换换行符。
content = content.Replace("\n","");
然后反序列化。祝你好运。
您可以使用以下代码段替换所有换行符
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"
}
}
然后复制更新后的字符串并使用编辑-特殊粘贴-粘贴JSON as classes。生成了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
实例嵌套在另一个 class 中。所以,为了解析你必须使用类似的东西
var response = JsonConvert.DeserializeObject<BookResponse>(content);
if (response.book != null)
{
}