C# 如何在C中正确使用url中的Json数据#
我访问过无数使用C#处理JSON和URL的页面,我知道所有的方法在逻辑上或多或少都是相同的 我都试过了,却找不到解决问题的办法 我正在使用fonoAPI获取移动设备的信息。API返回JSON格式的数据。我在代码中使用C# 如何在C中正确使用url中的Json数据#,c#,json,C#,Json,我访问过无数使用C#处理JSON和URL的页面,我知道所有的方法在逻辑上或多或少都是相同的 我都试过了,却找不到解决问题的办法 我正在使用fonoAPI获取移动设备的信息。API返回JSON格式的数据。我在代码中使用 var json=new webClient()。下载字符串(“https://fonoapi.freshpixl.com/v1/getdevice?device=i9505&token=MY_TOKEN_HERE");它正在工作并返回数据 数据= 当我试图处理数据时,我得到了一个
var json=new webClient()。下载字符串(“https://fonoapi.freshpixl.com/v1/getdevice?device=i9505&token=MY_TOKEN_HERE");代码>它正在工作并返回数据
数据=
当我试图处理数据时,我得到了一个错误,我无法判断我的代码到底出了什么问题。
让您知道这种简单的数据格式工作得非常好
var json2 = @"{ ""DeviceName"":""Samsung I9305 Galaxy S III"",""Brand"":""Samsung""}";
我的全部代码是:
using System;
using System.Net;
using Newtonsoft.Json;
using System.Web.Script.Serialization;
using System.Text.RegularExpressions;
using Newtonsoft.Json.Linq;
namespace lol
{
class Program
{
public static void Main(string[] args)
{
var json = new WebClient().DownloadString("https://fonoapi.freshpixl.com/v1/getdevice?device=i9505&token=MY_TOKEN_HERE");
//var json2 = @"{ ""DeviceName"":""Samsung I9305 Galaxy S III"",""Brand"":""Samsung""}";
RootObject r = JsonConvert.DeserializeObject<RootObject>(json);
Console.WriteLine(r.DeviceName);
Console.ReadLine();
}
public class RootObject
{
public string DeviceName { get; set; }
public string Brand { get; set; }
public string technology { get; set; }
public string gprs { get; set; }
public string edge { get; set; }
public string announced { get; set; }
public string status { get; set; }
public string dimensions { get; set; }
public string weight { get; set; }
public string sim { get; set; }
public string type { get; set; }
public string size { get; set; }
public string resolution { get; set; }
public string display_c { get; set; }
public string card_slot { get; set; }
public string alert_types { get; set; }
public string loudspeaker_ { get; set; }
public string wlan { get; set; }
public string bluetooth { get; set; }
public string gps { get; set; }
public string radio { get; set; }
public string usb { get; set; }
public string messaging { get; set; }
public string browser { get; set; }
public string java { get; set; }
public string features_c { get; set; }
public string battery_c { get; set; }
public string stand_by { get; set; }
public string talk_time { get; set; }
public string colors { get; set; }
public string sar_us { get; set; }
public string sar_eu { get; set; }
public string sensors { get; set; }
public string cpu { get; set; }
public string @internal { get; set; }
public string os { get; set; }
public string primary_ { get; set; }
public string video { get; set; }
public string secondary { get; set; }
public string speed { get; set; }
public string chipset { get; set; }
public string features { get; set; }
public string protection { get; set; }
public string gpu { get; set; }
public string multitouch { get; set; }
public string nfc { get; set; }
public string _2g_bands { get; set; }
public string _3_5mm_jack_ { get; set; }
public string _3g_bands { get; set; }
public string _4g_bands { get; set; }
}
}
}
错误:
您的响应包含一个RootObject
数组。使用以下代码反序列化数组:
var products = JsonConvert.DeserializeObject<List<RootObject>(json);
var products=JsonConvert.deserializeobject请将您的json示例包含为文本,并让我们知道错误消息从json文件中删除[(第一个字符)和](最后一个字符)
var products = JsonConvert.DeserializeObject<List<RootObject>(json);