2016-12-06 2 views
5

के साथ पार्स जेसन स्ट्रिंग मुझे बाहरी वेब सेवा से एक जेसन स्ट्रिंग मिलती है। मैं अपने डेटाबेस में events सरणी से ईवेंट को सहेजना चाहता हूं। मैं List<FacebookEvents> कैसे प्राप्त कर सकता हूं? अपने RootObject जो List<Event> और Metadata शामिलजेसन कन्वर्ट सी #

{ 
"events": [{ 
    "id": "163958810691757", 
    "name": "3Bridge Records presents inTRANSIT w/ David Kiss, Deep Woods, Eric Shans", 
    "coverPicture": "https://scontent.xx.fbcdn.net/t31.0-8/s720x720/13679859_10153862492796325_8533542782240254857_o.jpg", 
    "profilePicture": "https://scontent.xx.fbcdn.net/v/t1.0-0/c133.0.200.200/p200x200/13872980_10153862492796325_8533542782240254857_n.jpg?oh=a46813bbf28ad7b8bffb88acd82c7c71&oe=581EF037", 
    "description": "Saturday, August 20th.\n\nJoin the 3Bridge Records team for another night of sound and shenanigans - as we send Deep Woods & David Kiss out to Burning Man & belatedly celebrate Slav Ka's debut release on the label - \"Endless\" - out May 14th, featuring a remix by Mr. Shans.\n\nDavid Kiss (House of Yes)\nhttps://soundcloud.com/davidkiss\n\nDeep Woods (3Bridge Records)\nhttps://soundcloud.com/deep-woods\n\nEric Shans (3Bridge Records)\nhttps://soundcloud.com/eric-shans\n\nSlav Ka (3Bridge Records)\nhttps://soundcloud.com/slinkyslava\n\nFree before 12, $10 after (+ 1 comp well drink). $5 presale available on RA.\n\nhttps://www.residentadvisor.net/event.aspx?863815\n\nStay dope, Brooklyn.", 
    "distance": "203", 
    "startTime": "2016-08-20T22:00:00-0400", 
    "timeFromNow": 481946, 
    "stats": { 
     "attending": 44, 
     "declined": 3, 
     "maybe": 88, 
     "noreply": 1250 
    }, 
    "venue": { 
     "id": "585713341444399", 
     "name": "TBA Brooklyn", 
     "coverPicture": "https://scontent.xx.fbcdn.net/v/t1.0-9/s720x720/13932666_1397749103574148_4391608711361541993_n.png?oh=2d82be3a458d1ce9ac8fab47cdbc6e26&oe=585E6545", 
     "profilePicture": "https://scontent.xx.fbcdn.net/v/t1.0-1/p200x200/12049351_1300865083262551_8221231831784471629_n.jpg?oh=a30798841ad60dfe5cfabaa4e803c3ad&oe=5854DFB9", 
     "location": { 
      "city": "Brooklyn", 
      "country": "United States", 
      "latitude": 40.711217064583, 
      "longitude": -73.966384349735, 
      "state": "NY", 
      "street": "395 Wythe Ave", 
      "zip": "11249" 
     } 
    } 
}, 
... 
], 
"metadata": { 
    "venues": 1, 
    "venuesWithEvents": 1, 
    "events": 4 
}} 




class FacebookEvents 
    { 
     //[JsonProperty(PropertyName = "id")] 
     public string id { get; set; } 
     public string name { get; set; } 
     public string coverPicture { get; set; } 
     public string profilePicture { get; set; } 
     public string description { get; set; } 
     public string distance { get; set; } 
     public string startTime { get; set; } 
     public string timeFromNow { get; set; } 
     public Stats stats { get; set; } 

    } 

    class Stats { 
     public string attending { get; set; } 
     public string declined { get; set; } 
     public string maybe { get; set; } 
     public string noreply { get; set; } 

    } 
    class Venue 
    { 
     public string id { get; set; } 
     public string name { get; set; } 
     public string coverPicture { get; set; } 
     public string profilePicture { get; set; } 
     public Location location { get; set; } 

    } 
    class Location 
    { 
     public string city { get; set; } 
     public string country { get; set; } 
     public string latitude { get; set; } 
     public string longitude { get; set; } 
     public string state { get; set; } 
     public string street { get; set; } 
     public string zip { get; set; } 

    } 
+0

_ "सूची my_obj = JsonConvert उपयोग करने में सक्षम हो जाएगा। DeserializeObject <फेसबुकEvents> (jsonString); यह काम नहीं करता है। "_ - यह ** ** संकलित नहीं करेगा **। – stuartd

+0

आपको जड़ ऑब्जेक्ट को फेसबुक एवेन्ट्स [] – Nkosi

उत्तर

3

आप को परिभाषित करने के याद कर रहे हैं:

मेरे वर्तमान प्रयास काम नहीं करता: List<FacebookEvents> my_obj = JsonConvert.DeserializeObject <FacebookEvents> (jsonString);

स्रोत json। Full Example

public class RootObject 
{ 
    public List<Event> events { get; set; } 
    public Metadata metadata { get; set; } 
} 

public class Stats 
{ 
    public int attending { get; set; } 
    public int declined { get; set; } 
    public int maybe { get; set; } 
    public int noreply { get; set; } 
} 

public class Location 
{ 
    public string city { get; set; } 
    public string country { get; set; } 
    public double latitude { get; set; } 
    public double longitude { get; set; } 
    public string state { get; set; } 
    public string street { get; set; } 
    public string zip { get; set; } 
} 

public class Venue 
{ 
    public string id { get; set; } 
    public string name { get; set; } 
    public string coverPicture { get; set; } 
    public string profilePicture { get; set; } 
    public Location location { get; set; } 
} 

public class Event 
{ 
    public string id { get; set; } 
    public string name { get; set; } 
    public string coverPicture { get; set; } 
    public string profilePicture { get; set; } 
    public string description { get; set; } 
    public string distance { get; set; } 
    public string startTime { get; set; } 
    public int timeFromNow { get; set; } 
    public Stats stats { get; set; } 
    public Venue venue { get; set; } 
} 

public class Metadata 
{ 
    public int venues { get; set; } 
    public int venuesWithEvents { get; set; } 
    public int events { get; set; } 
} 

यह काम करेगा:

var result = JsonConvert.DeserializeObject<RootObject>(jsonString); 

संपादित करें:

सबसे पहले इस JSON है, आपके द्वारा स्थान जानकारी ले सकते हैं।

foreach(var item in result.events) 
{ 
    Console.WriteLine(item.venue.name); 
} 
+0

टीएनएक्स की त्वरित प्रतिक्रिया के लिए एक घटना की संपत्ति की आवश्यकता होती है। परिणामस्वरूप एक्सएमएल का केवल स्थान भाग प्राप्त करने के लिए मुझे क्या करना है? – LoZo

+0

@LoZo संपादन की जांच करें। – mybirthname

+0

हाँ यह तार्किक है लेकिन मैं शायद jpath के साथ कुछ उदाहरण देखना चाहता हूं। जॉबजेक्ट my_obj2 = JsonConvert.DeserializeObject (जेसनस्ट्रिंग); आईनेमेरेबल स्थल = my_obj2.SelectTokens ("events.venue"); मैं सभी घटनाओं में केवल स्थान नोड्स प्राप्त करना चाहता हूं। मैं थोड़ी सी जटिलता कर रहा हूं क्योंकि मैं समझना चाहता हूं कि यह कैसे काम करता है :) – LoZo

1

आप एक जड़ वस्तु एक events संपत्ति संग्रह स्टोर करने के लिए

public class RootObject { 

    public IList<FacebookEvents> events {get;set;} 

} 

है कि जरूरत है और फिर आप घटनाओं

var root = JsonConvert.DeserializeObject<RootObject>(jsonString); 
var events = root.events; 
संबंधित मुद्दे