【问题标题】:ERROR:Additional information: Unexpected character encountered while parsing value: W. Path '', line 2, position 1错误:附加信息:解析值时遇到意外字符:W. Path '', line 2, position 1
【发布时间】:2016-01-14 12:23:01
【问题描述】:

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

附加信息:解析时遇到意外字符 value: W. Path '', line 2, position 1.

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

【问题讨论】:

    标签: c# php json datagridview


    【解决方案1】:

    试试这个:

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-05
      • 2022-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-17
      • 2016-11-10
      相关资源
      最近更新 更多