如何在C#

时间:2017-07-22 12:24:06

标签: c# json

我已经在C#中访问了无数的JSON和URL处理页面,我知道所有正在运行的方法在逻辑上或多或少相同。

我尝试了所有这些,但我找不到解决问题的方法。

我正在使用fonoAPI获取移动设备的信息。 API以JSON格式返回数据。我在我的代码中使用了 var json = new webClient().DownloadString("https://fonoapi.freshpixl.com/v1/getdevice?device=i9505&token=MY_TOKEN_HERE");它正在工作并返回数据。

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

当我尝试处理数据时,我收到一个错误,我无法弄清楚我的代码是什么问题。 让您知道这种简单的数据格式正常运行

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

我的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)"}]

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

1 个答案:

答案 0 :(得分:0)

您的回复包含RootObject数组。使用此代码反序列化数组:

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

在图像中的json示例处拍摄。 Json以方括号[开头,这意味着它是一个数组。