我使用下面的代码来使用Newtonsoft.Json以JSON格式获取数据:
我创建JSON的代码是
Dim sb As New StringBuilder()
Dim sw As New StringWriter(sb)
Dim writer As JsonWriter = New JsonTextWriter(sw)
writer.WriteStartArray()
For Each elements As JToken In result
If i = 0 Then
i += 1
Continue For
End If
For Each aaa As JToken In elements
writer.WriteStartObject()
Dim pmid = aaa("uid").Value(Of String)
Dim issn = aaa("issn").Value(Of String)
Dim essn = aaa("essn").Value(Of String)
Dim source = aaa("source").Value(Of String)
Dim sortpubdate = aaa("sortpubdate").Value(Of String)
writer.WritePropertyName("pmid")
writer.WriteValue(pmid)
writer.WritePropertyName("journal")
writer.WriteValue(source)
writer.WritePropertyName("issn")
writer.WriteValue(issn)
writer.WritePropertyName("essn")
writer.WriteValue(essn)
writer.WritePropertyName("sortpubdate")
writer.WriteValue(sortpubdate)
writer.WritePropertyName("pubYear")
writer.WriteEndObject()
Next
Next
writer.Close()
Return sb.ToString输出代码是
public HttpResponseMessage Get()
{
string yourJson = GetJsonFromSomewhere();
var response = this.Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent(yourJson, Encoding.UTF8, "application/json");
return response;
}我现在的输出是。
[{"pmid":"29241721","issn":"0749-3797","essn":"1873-2607","journal":"Am J Prev Med","pubYear":"2018","ImpactFactor":" 4.127"},{"pmid":"28987617","issn":"0166-4328","essn":"1872-7549","journal":"Behav Brain Res","pubYear":"2018","ImpactFactor":" 3.173"},有办法缩进我的输出吗?
[{
"pmid": "29241721",
"issn": "0749-3797",
"essn": "1873-2607",
"journal": "Am J Prev Med",
"pubYear": "2018",
"ImpactFactor": " 4.127"
}, {有什么办法我可以像下面这样做吗?
this.SerializerSettings.Formatting = Formatting.Indented;客户端只是一个简单的浏览器URL,可以得到响应
发布于 2019-11-03 18:08:37
我得到了我需要的
Dim writer As JsonWriter = New JsonTextWriter(sw)
writer.Formatting = Newtonsoft.Json.Formatting.Indented发布于 2019-11-03 15:06:52
您应该在序列化时缩进对象,然后在任何地方使用它。如果GetJsonFormatSomewhere()是您的序列化方法,则应该在下面的方法中对对象进行序列化:
var serializedObject = JsonConvert.SerializeObject(data, Formatting.Indented);然后将缩进对象传递给StringContent方法。
备注:最好在客户端缩进您的输出,这样您的数据包大小就会保持较小,并且性能友好。
https://stackoverflow.com/questions/58681137
复制相似问题