我的應用程式收到一個 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 的經驗。任何幫助,將不勝感激

uj5u.com熱心網友回復:
首先從你的 json 定義你的 c# 類。你可以使用
嘿嘿,一點都不差!!!!
現在附加列 - 它們在上面不起作用。
但這意味著我們必須嵌套并將網格放入上述 DataList/Repeater 控制元件中。
好的,讓我們這樣做。所以在上面,就在前兩個標簽的正下方,我們把這個放進去:
<div style="margin-left:25px">
<asp:GridView ID="GridView2" runat="server"
CssClass="table table-condensed"></asp:GridView>
</div>
現在,我們必須把它填滿。我真的很想保留這段代碼,但我想不出更好的方法(當您需要時,linq 和 lamda 運算式專家在哪里????)。
However, this seems to work:
So we are going to use what is called the data bind event. This is much the same for a gridview, listview, repeater etc. (the beauty of .net is once you learn one, then you know them all).
So, we will get the current data row we are bindings.
Create a fake table for the 4 multi-value columns shove the data into that fake table, and then shove the table into that gridview.
It not TOO much code, but VERY close to the limits of what is practical on SO.
So, this works:
protected void MyDataList_ItemDataBound(object sender, DataListItemEventArgs e)
{
GridView gv = (GridView)e.Item.FindControl("GridView2");
DataRowView OneDataRow = (DataRowView)e.Item.DataItem;
DataTable rstData = new DataTable();
rstData.Columns.Add("tokens");
rstData.Columns.Add("pos");
rstData.Columns.Add("ner");
rstData.Columns.Add("lemmas");
for (int ChildRow = 0;ChildRow < ((string[])OneDataRow["tokens"]).Length;ChildRow ) {
// add new row
DataRow NewRow = rstData.NewRow();
foreach(DataColumn myCol in rstData.Columns)
{
// add each column value
NewRow[myCol.ColumnName] = ((string[])OneDataRow[myCol.ColumnName])[ChildRow].ToString();
}
rstData.Rows.Add(NewRow);
}
gv.DataSource = rstData;
gv.DataBind();
}
not too bad, and not too messy.
So, I think you can see what we are doing here.
Output:

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/346620.html
上一篇:如何解決“'default_aspx'不包含'Button1_Click'的定義”錯誤?
下一篇:如何查找特定字串和動態字串
