gpt4 book ai didi

c# - foreach 循环中的 String.Format 用于在同一行中打印

转载 作者:太空宇宙 更新时间:2023-11-03 22:49:14 24 4
gpt4 key购买 nike

我有一个函数可以读取 Json 并将其反序列化。 JSON 是嵌套的,我想要的是在同一行中打印一些嵌套值。

我的 JSON 是:

{
"number": 123,
"person": [{
"phone": 864556,
"name": "John D",
"address": "N.Y",
"email": "test@test.com"
},
{
"phone": 19845,
"name": "Mary S",
"address": "N.Y",
"email": "testmary@test.com"
},
{
"phone": 34936,
"name": "Alex N",
"address": "Santiago",
"email": "alex@test.com"
},
{
"phone": 197478,
"name": "Bill McK",
"address": "London",
"email": "test@example.com"
}]
}

我有这个

foreach (var num in JMain.person)
{
debugOutput(String.Format("<{0};>", num.phone));
}

private void debugOutput(string strDebugText)
{
try
{
outPut += strDebugText + Environment.NewLine;
System.Diagnostics.Debug.Write(strDebugText + Environment.NewLine);

}
catch(Exception ex)
{
System.Diagnostics.Debug.Write(ex.Message.ToString() + Environment.NewLine);
}
}

其中 debugOutput 是我读取 JSON 并打印它的函数。我想要的是在一行中打印所有电话。使用 for 循环和 String.Format 我可以将它们一个接一个地打印出来。也许使用 String.Format 是不对的,还有另一种方法吗?

谢谢

最佳答案

如果你想把所有的输出都放在一个字符串中,你可以这样做。根据您的 debugOutput 函数中的内容,可能有更简单的方法。

string outputstring = "";
foreach (var num in JMain.person)
{
outputstring += String.Format("<{0};>", num.phone);
}
debugOutput(outputstring);

关于c# - foreach 循环中的 String.Format 用于在同一行中打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48299576/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com