简体   繁体   English

如何在C#中正确使用URL中的Json数据

[英]How properly use Json data from url in C#

I have visited countless pages with JSON and URL handling in C# and I know that all of the methods that were working are more or less the same in logic. 我用C#访问了无数带有JSON和URL处理的页面,并且我知道所有起作用的方法在逻辑上或多或少都是相同的。

I tried them all and I can't find a solution to my problem. 我都尝试了所有方法,但找不到解决方案。

I am using the fonoAPI to get information for a mobile device. 我正在使用fonoAPI来获取有关移动设备的信息。 The API returns the data in JSON. API以JSON返回数据。 I use in my code the var json = new webClient().DownloadString("https://fonoapi.freshpixl.com/v1/getdevice?device=i9505&token=MY_TOKEN_HERE"); 我在代码中使用了var json = new webClient().DownloadString("https://fonoapi.freshpixl.com/v1/getdevice?device=i9505&token=MY_TOKEN_HERE"); which it is working and returning the data. 它正在工作并返回数据。

Data = http://imgur.com/a/U5Cje 数据= http://imgur.com/a/U5Cje

When i try to proccess the data i am getting an error which i cant figure what it is wrong with my code. 当我尝试处理数据时,出现错误,我无法弄清楚我的代码有什么问题。 let you know that this simple format of data is working perfectly 让您知道,这种简单的数据格式运行良好

var json2 = @"{ ""DeviceName"":""Samsung I9305 Galaxy S III"",""Brand"":""Samsung""}";

My whole code is : 我的整个代码是:

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; }
        }
    }
}

My JSON file : 我的JSON文件:

[{"DeviceName":"Samsung I9305 Galaxy S III","Brand":"Samsung","technology":"GSM \/ HSPA \/ LTE","gprs":"Class 12","edge":"Class 12","announced":"2012, September","status":"Available. Released 2012, September","dimensions":"136.6 x 70.6 x 8.6 mm (5.38 x 2.78 x 0.34 in)","weight":"131 g (4.62 oz)","sim":"Micro-SIM","type":"Super AMOLED capacitive touchscreen, 16M colors","size":"4.8 inches (~65.9% screen-to-body ratio)","resolution":"720 x 1280 pixels (~306 ppi pixel density)","display_c":"- TouchWiz UI\r\n  ","card_slot":"microSD, up to 64 GB","alert_types":"Vibration; MP3, WAV ringtones","loudspeaker_":"Yes","wlan":"Wi-Fi 802.11 a\/b\/g\/n, dual-band, Wi-Fi Direct, DLNA, hotspot","bluetooth":"v4.0, A2DP, EDR, apt-X","gps":"Yes, with A-GPS, GLONASS","radio":"No","usb":"microUSB v2.0 (MHL TV-out), USB Host","messaging":"SMS(threaded view), MMS, Email, Push Mail, IM","browser":"HTML, Adobe Flash","java":"No","features_c":"- ANT+ support\r\n  - S-Voice natural language commands and dictation\r\n  - Dropbox (50 GB cloud storage)\r\n  - Active noise cancellation with dedicated mic\r\n  - MP4\/DivX\/XviD\/WMV\/H.264 player\r\n  - MP3\/WAV\/eAAC+\/AC3\/FLAC player\r\n  - Photo\/video editor\r\n  - Document viewer","battery_c":"Removable Li-Ion 2100 mAh battery","stand_by":"Up to 580 h (2G) \/ Up to 390 h (LTE)\/ 510 h (3G)","talk_time":"Up to 16 h (2G) \/ Up to 11 h (3G)","colors":"Titanium Gray, Sapphire Black","sar_us":"  0.41 W\/kg (head) &nbsp; &nbsp; 0.87 W\/kg (body) &nbsp; &nbsp; ","sar_eu":"  0.24 W\/kg (head) &nbsp; &nbsp; ","sensors":"Accelerometer, gyro, proximity, compass, barometer","cpu":"Quad-core 1.4 GHz Cortex-A9","internal":"16 GB, 2 GB RAM","os":"Android OS, v4.1.1 (Jelly Bean), v4.3 (Jelly Bean), upgradable to v4.4.4 (KitKat)","primary_":"8 MP, f\/2.6, autofocus, LED flash","video":"1080p@30fps","secondary":"1.9 MP, f\/2.6, 720p@30fps","speed":"HSPA 42.2\/5.76 Mbps, LTE Cat3 100\/50 Mbps","chipset":"Exynos 4412 Quad","features":"1\/3\" sensor size, geo-tagging, touch focus, face\/smile detection","protection":"Corning Gorilla Glass 2","gpu":"Mali-400MP4","multitouch":"Yes","nfc":"Yes","_2g_bands":"GSM 850 \/ 900 \/ 1800 \/ 1900 ","_3_5mm_jack_":"Yes","_3g_bands":"HSDPA 850 \/ 900 \/ 2100 ","_4g_bands":"LTE band 3(1800), 7(2600), 20(800)"}]

Error: http://imgur.com/a/EvCkF 错误: http//imgur.com/a/EvCkF

Your response contains an array of RootObject . 您的响应包含一个RootObject数组。 Use this code to deserialize array: 使用以下代码反序列化数组:

var products = JsonConvert.DeserializeObject<List<RootObject>(json);

Loot at your json example in the image. 在图片中找到您的json示例。 Json starts with square bracket [ , this mean it's an array. Json以方括号[开头,这意味着它是一个数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM