2013-08-28 18 views
13

मैं का उपयोग कर सी # एक आराम एपीआई json प्रतिक्रिया से कोई मान खींच कोशिश कर रहा हूँपार्सिंग Json सी # में बाकी एपीआई प्रतिक्रिया

मैं अनुसरण कोड

client.BaseUrl = "https://api.cloud.appcelerator.com"; 
request.Resource = "/v1/chats/create.json?key=" + cac.AppCode.ToString(); 
request.Method = Method.POST; 
request.AddUrlSegment("appkey", "key"); 
var response = client.Execute(request); 

"प्रतिक्रिया" में मैं एक json मिलता है इस प्रकार सामग्री: "521cfcd840926a0b3500449e":

{ 
    "meta": { 
    "code": 200, 
    "status": "ok", 
    "method_name": "createChatMessage" 
    }, 
    "response": { 
    "chats": [ 
     { 
     "id": "521cfcd840926a0b3500449e", 
     "created_at": "2013-08-27T19:24:08+0000", 
     "updated_at": "2013-08-27T19:24:08+0000", 
     "message": " join to the chat group, welcome …", 
     "from": { 
      "id": "520f41e125e74b0b2400130a", 
      "first_name": "Administrator", 
      "created_at": "2013-08-17T09:26:57+0000", 
      "updated_at": "2013-08-27T19:23:10+0000", 
      "external_accounts": [ 

      ], 
      "email": "[email protected]", 
      "confirmed_at": "2013-08-17T09:26:57+0000", 
      "username": "admin", 
      "admin": "true", 
      "stats": { 
      "photos": { 
       "total_count": 0 
      }, 
      "storage": { 
       "used": 0 
      } 
      } 
     }, 
     "chat_group": { 
      "id": "521cfcd840926a0b3500449d", 
      "created_at": "2013-08-27T19:24:08+0000", 
      "updated_at": "2013-08-27T19:24:08+0000", 
      "message": " join to the chat group, welcome …", 
      "participate_users": [ 
      { 
       "id": "520f41e125e74b0b2400130a", 
       "first_name": "Administrator", 
       "created_at": "2013-08-17T09:26:57+0000", 
       "updated_at": "2013-08-27T19:23:10+0000", 
       "external_accounts": [ 

       ], 
       "email": "[email protected]", 
       "confirmed_at": "2013-08-17T09:26:57+0000", 
       "username": "admin", 
       "admin": "true", 
       "stats": { 
       "photos": { 
        "total_count": 0 
       }, 
       "storage": { 
        "used": 0 
       } 
       } 
      } 
      ] 
     } 
     } 
    ] 
    } 
} 

कैसे मैं अनुसरण मूल्य "आईडी" खींच करते हैं? मैं सी # का उपयोग कर रहा हूं ....

+1

वास्तव में कोशिश कर आम तौर पर एक अच्छी शुरुआत है के रूप में में – x4rf41

+0

http://stackoverflow.com/questions/6620165/how-to-parse-json-in-c .... (गूगल भी एक बहुत मदद करता है), एक पार्सर का उपयोग करें (इस समय आपने इसका कोई सबूत नहीं दिखाया है)। इससे संबंधित अन्य धागे भी हैं। क्या आपने खोज करने की कोशिश की है? यदि हां, तो परिणाम क्यों काम नहीं करते? – Arran

उत्तर

31

1> इस namspace जोड़ें। Newtonsoft.Json.Linq का उपयोग कर ;

2> इस स्रोत कोड का उपयोग करें।

JObject joResponse = JObject.Parse(response);     
JObject ojObject = (JObject)joResponse["response"]; 
JArray array= (JArray)ojObject ["chats"]; 
int id = Convert.ToInt32(array[0].toString()); 
1

एक सी # कक्षा बनाएं जो आपके जेसन में मैप करें और Newsoft JsonConvert का उपयोग Deserialise करने के लिए करें।

उदाहरण के लिए:

public Class MyResponse 
{ 
    public Meta Meta { get; set; } 
    public Response Response { get; set; } 
} 
17
  1. वर्गों है कि आपके डेटा,
  2. तो JSON.NET का उपयोग नियमित रूप से सी # वस्तुओं के लिए JSON डेटा कन्वर्ट करने के लिए मेल खाते बनाएँ।

चरण 1: एक महान उपकरण - http://json2csharp.com/ - यह द्वारा उत्पन्न परिणाम हैं नीचे

चरण 2: JToken.Parse(...).ToObject<RootObject>()

public class Meta 
{ 
    public int code { get; set; } 
    public string status { get; set; } 
    public string method_name { get; set; } 
} 

public class Photos 
{ 
    public int total_count { get; set; } 
} 

public class Storage 
{ 
    public int used { get; set; } 
} 

public class Stats 
{ 
    public Photos photos { get; set; } 
    public Storage storage { get; set; } 
} 

public class From 
{ 
    public string id { get; set; } 
    public string first_name { get; set; } 
    public string created_at { get; set; } 
    public string updated_at { get; set; } 
    public List<object> external_accounts { get; set; } 
    public string email { get; set; } 
    public string confirmed_at { get; set; } 
    public string username { get; set; } 
    public string admin { get; set; } 
    public Stats stats { get; set; } 
} 

public class ParticipateUser 
{ 
    public string id { get; set; } 
    public string first_name { get; set; } 
    public string created_at { get; set; } 
    public string updated_at { get; set; } 
    public List<object> external_accounts { get; set; } 
    public string email { get; set; } 
    public string confirmed_at { get; set; } 
    public string username { get; set; } 
    public string admin { get; set; } 
    public Stats stats { get; set; } 
} 

public class ChatGroup 
{ 
    public string id { get; set; } 
    public string created_at { get; set; } 
    public string updated_at { get; set; } 
    public string message { get; set; } 
    public List<ParticipateUser> participate_users { get; set; } 
} 

public class Chat 
{ 
    public string id { get; set; } 
    public string created_at { get; set; } 
    public string updated_at { get; set; } 
    public string message { get; set; } 
    public From from { get; set; } 
    public ChatGroup chat_group { get; set; } 
} 

public class Response 
{ 
    public List<Chat> chats { get; set; } 
} 

public class RootObject 
{ 
    public Meta meta { get; set; } 
    public Response response { get; set; } 
} 
+0

दोस्त, json2csharp वेबसाइट अद्भुत है! साझा करने के लिए धन्यवाद! – Hajjat

+0

मैं वही कर रहा हूं लेकिन ऐसा लगता है कि यह 'सी #' कोडिंग कन्वेंशन का उल्लंघन करता है जैसे 'पूंजी पत्र शुरू किए बिना गुण'। क्या आप इस उदाहरण को और भी बेहतर बनाने की सलाह दे सकते हैं? – CodeIt

संबंधित मुद्दे