【发布时间】:2021-12-16 13:04:41
【问题描述】:
我的应用程序收到一个 json 字符串。我希望能够以一种很好的格式化方式显示这个字符串。真的,我什至不知道该问什么问题,这就是我的问题的根源。
这是我收到的字符串示例:
[{"sentence" : "Goldman Dukes is testing to see whether our request functionality works for the upcoming sprint.","sentenceNbr" : "1","tokens" : ["Goldman", "Dukes", "is", "testing", "to", "see", "whether", "our", "request", "functionality", "works", "for", "the", "upcoming", "sprint", "."],"pos" : ["NNP", "NNP", "VBZ", "VBG", "TO", "VB", "IN", "PRP$", "NN", "NN", "VBZ", "IN", "DT", "VBG", "NN", "."],"ner" : ["PERSON", "PERSON", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O"],"lemmas" : ["Goldman", "Dukes", "be", "test", "to", "see", "whether", "we", "request", "functionality", "work", "for", "the", "upcome", "sprint", "."]},{"sentence" : "Nick Wills is a great guy.","sentenceNbr" : "2","tokens" : ["Nick", "Wills", "is", "a", "great", "guy", "."],"pos" : ["NNP", "NNP", "VBZ", "DT", "JJ", "NN", "."],"ner" : ["PERSON", "PERSON", "O", "O", "O", "O", "O"],"lemmas" : ["Nick", "Wills", "be", "a", "great", "guy", "."]},{"sentence" : "He lives in Northern Virginia.","sentenceNbr" : "3","tokens" : ["He", "lives", "in", "Northern", "Virginia", "."],"pos" : ["PRP", "VBZ", "IN", "NNP", "NNP", "."],"ner" : ["O", "O", "O", "LOCATION", "STATE_OR_PROVINCE", "O"],"lemmas" : ["he", "live", "in", "Northern", "Virginia", "."]}]
我收到的字符串与上面完全相同,没有空格或其他格式帮助。这是一个稍微易读的版本:
[
{
"sentence" : "Goldman Dukes is testing to see whether our request functionality works for the upcoming sprint.",
"sentenceNbr" : "1",
"tokens" : ["Goldman", "Dukes", "is", "testing", "to", "see", "whether", "our", "request", "functionality", "works", "for", "the", "upcoming", "sprint", "."],
"pos" : ["NNP", "NNP", "VBZ", "VBG", "TO", "VB", "IN", "PRP$", "NN", "NN", "VBZ", "IN", "DT", "VBG", "NN", "."],
"ner" : ["PERSON", "PERSON", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O"],
"lemmas" : ["Goldman", "Dukes", "be", "test", "to", "see", "whether", "we", "request", "functionality", "work", "for", "the", "upcome", "sprint", "."]
},
{
"sentence" : "Nick Wills is a great guy.",
"sentenceNbr" : "2",
"tokens" : ["Nick", "Wills", "is", "a", "great", "guy", "."],
"pos" : ["NNP", "NNP", "VBZ", "DT", "JJ", "NN", "."],
"ner" : ["PERSON", "PERSON", "O", "O", "O", "O", "O"],
"lemmas" : ["Nick", "Wills", "be", "a", "great", "guy", "."]
},
{
"sentence" : "He lives in Northern Virginia.",
"sentenceNbr" : "3",
"tokens" : ["He", "lives", "in", "Northern", "Virginia", "."],
"pos" : ["PRP", "VBZ", "IN", "NNP", "NNP", "."],
"ner" : ["O", "O", "O", "LOCATION", "STATE_OR_PROVINCE", "O"],
"lemmas" : ["he", "live", "in", "Northern", "Virginia", "."]
}
]
我的最终目标是以 gridview 类型的格式显示这些数据,但现在我会满足于弄清楚如何以“漂亮”的方式显示它,如上所述。
我非常熟悉使用 C#,但没有使用 JSON 的经验。任何帮助将不胜感激
【问题讨论】:
-
也许这个:nuget.org/packages/FracturedJson 但一般来说,这里认为要求工具推荐的问题是题外话。
-
你能模拟出你期望这个 GV 的样子吗,可以截个 Excel 吗?
-
既然您熟悉 C#,这应该是轻而易举的事。想象一下 JSON 是 XML,您将把它反序列化为对象,就像您使用 XML 一样。或者想象它是您从数据库加载的数据。使用 JSON 中的键(“句子”、“令牌”等)等属性定义您的类,然后使用 System.Text.Json (docs.microsoft.com/en-us/dotnet/api/…) 或 Newtonsoft.Json (newtonsoft.com/json) 转换 JSON串成对象。然后像显示任何其他 c# 对象一样显示这些对象。
-
@CaiusJard 我上传了这可能喜欢的样本
-
@gnud 属性标记、pos、ner、lemmas 的数据类型都是字符串 [] 吗?