错误:其他信息:解析值时遇到意外字符:W. 路径 '',第 2 行,位置 1


ERROR:Additional information: Unexpected character encountered while parsing value: W. Path '', line 2, position 1

类型为"Newtonsoft.Json.JsonReaderException"的未处理异常 发生在Newtonsoft.Json.dll

其他信息:分析时遇到意外字符 值:W. 路径 '',第 2 行,位置 1。

这对我来说是一个问题,因为我使用的是应用程序(用 C# 开发)。我发现获取信息的方法是:创建一些PHP文件,这些文件托管在我的服务器上,将本地连接到数据库并以JSON格式返回信息。然后,我需要更改我的 C# 应用程序以使用这些 JSON。在 c# 中使用 Json.NET 将 PHP 中的 JSON 显示到 DataGridView 中

编码:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace HTTTPRESPONSE
{
class User
{
    [JsonProperty("userid")]
    public string userid { get; set; }
    [JsonProperty("password")]
    public string password { get; set; }
    [JsonProperty("first_anme")]
    public string first_name { get; set; }
    [JsonProperty("last_name")]
    public string last_name { get; set; }
    [JsonProperty("role")]
    public string role { get; set; }
    [JsonProperty("active")]
    public string active { get; set; }
}
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        WebClient wc = new WebClient();
        var data = wc.DownloadString("http://***.**.***.***/data.php");
        List<User> users = JsonConvert.DeserializeObject<List<User>>(data);
        dataGridView1.DataSource = users;
      }
   }
 }

行中的错误:

List<User> users = JsonConvert.DeserializeObject<List<User>>(data);

我的参考是:http://www.codeproject.com/Articles/609027/Displaying-JSON-from-PHP-into-a-DataGridView-using

试试这个:

var request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = WebRequestMethods.Http.Get;
            request.Accept = "application/json";
            WebResponse response = request.GetResponse();
            Stream stream = response.GetResponseStream();
            StreamReader streamreader = new StreamReader(stream);
            String json = streamreader.ReadToEnd();
            List<User> users = JsonConvert.DeserializeObject<List<User>>(json);
            dataGridView1.DataSource = users;