簡體   English   中英

錯誤:附加信息:解析值時遇到意外字符:W。路徑'',第2行,位置1

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

Newtonsoft.Json.dll中發生未處理的“Newtonsoft.Json.JsonReaderException”類型異常

附加信息:解析值時遇到意外的字符:W。路徑'',第2行,位置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 havenGridView-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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM