我有一个简单的PHP脚本,该脚本在后端使用json_encode方法输出字符串数组。它输出如下:
["8090123","8090456","8090789","8090321","8090654"]
现在,我想在我的C#代码中解析它,但似乎无法正确解析它。
以下是我的C#代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
using (WebClient wc = new WebClient())
{
string myString = wc.DownloadString("https://magforex.biz/list.php");
string[] words = myString.Split(',');
for (var i = 0; i < words.Length; i++)
{
// i just hardcoded the index for now
Console.WriteLine(words[0]);
break;
}
Console.ReadLine();
}
}
}
}
该代码运行得很好,但它输出以下结果["8090123"
,这不是我想要的。
抱歉,我刚开始使用C#,对它的了解有限。
我想要的只是字符串,8090123
以便以后进行比较。
感谢您的事先帮助。
使用适当的JSON解析库(例如Newtonsoft.Json)将JSON字符串解析为静态类型的类模型:
string[] words = JsonConvert.DeserializeObject<string[]>(myString);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句