ana*_*nam 2 php c# json json.net
我有一个简单的PHP脚本,使用后端的json_encode方法输出一个字符串数组.它输出如下:
["8090123","8090456","8090789","8090321","8090654"]
Run Code Online (Sandbox Code Playgroud)
现在,我想在我的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();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
代码运行得很好,但它输出的结果["8090123"不是我想要的结果.
抱歉,因为我刚刚开始使用C#并且对此知之甚少.
我想要的只是字符串,8090123以便我以后可以进行比较.
感谢您提前帮忙.
使用适当的JSON解析库(如Newtonsoft.Json)将JSON字符串解析为静态类型的类模型:
string[] words = JsonConvert.DeserializeObject<string[]>(myString);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
668 次 |
| 最近记录: |