How to extract a JSON Array entry in C# for Infopath -
i new programming. wanted create c# infopath button send sms when clicked. went except response server. able display response, want display part of web response. here code:
using microsoft.office.infopath; using system; using system.xml; using system.xml.xpath; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using system.io; using system.net; using system.web; using system.web.script.serialization; //using system.json; using newtonsoft.json; namespace sms_form_fields { public partial class formcode { // member variables not supported in browser-enabled forms. // instead, write , read these values formstate // dictionary using code such following: // // private object _membervariable // { // // { // return formstate["_membervariable"]; // } // set // { // formstate["_membervariable"] = value; // } // } // note: following procedure required microsoft infopath. // can modified using microsoft infopath. public void internalstartup() { eventmanager.formevents.submit += new submiteventhandler(formevents_submit); } public void formevents_submit(object sender, submiteventargs e) { var uribuilder = new uribuilder("http://smsgateway.me/api/v3/messages/send/"); var parameters = httputility.parsequerystring(string.empty); parameters["email"] = maindatasource.createnavigator().selectsinglenode("/my:myfields/my:email", namespacemanager).value; parameters["password"] = maindatasource.createnavigator().selectsinglenode("/my:myfields/my:password", namespacemanager).value; parameters["device"] = maindatasource.createnavigator().selectsinglenode("/my:myfields/my:device", namespacemanager).value; ; parameters["number"] = maindatasource.createnavigator().selectsinglenode("/my:myfields/my:mobilenumber", namespacemanager).value; parameters["message"] = maindatasource.createnavigator().selectsinglenode("/my:myfields/my:sms_to_be_sent", namespacemanager).value; ; uribuilder.query = parameters.tostring(); //uribuilder.fragment = "some_fragment"; uri finalurl = uribuilder.uri; var request = webrequest.create(finalurl); // response. webresponse result = request.getresponse(); // stream containing content returned server. stream datastream = result.getresponsestream(); // open stream using streamreader easy access. streamreader reader = new streamreader(datastream); // read content. string responsefromserver = reader.readtoend(); dynamic stuff = jsonconvert.deserializeobject(responsefromserver); var result1 = stuff.result; var success1 = result1.success; string value = convert.tostring(success1); // display content. system.windows.forms.messagebox.show(value); } // clean streams. reader.close(); datastream.close(); result.close(); } }
here json example success
{ "success": true, "result": { "success": [ { "id": "308", "device_id": "4", "message": "hello world!", "status": "pending", "send_at": "1414624856", "queued_at": "0", "sent_at": "0", "delivered_at": "0", "expires_at": "1414634856", "canceled_at": "0", "failed_at": "0", "received_at": "0", "error": "none", "created_at": "1414624856", "contact": { "id": "14", "name": "phyllis turner", "number": "+447791064713" } } ], "fails": [ ] } }
here json example failure
{ "success": true, "result": { "success": [ ], "fails": [ "number": "+44771232343" "message": "hello world!", "device": 1 "errors": { "device": ["the selected device invalid"], } ] } }
all want if succeeds should able display part of json, example id, device id or message , if failure should able display part of json example device, number or errors.
updated failure response after stevej pointed:
{ "success": true, "result": { "success": [], "fails": [ { "email": "abc@xyz.com", "password": "anypassword", "device": 1, "number": "+44771232343", "message": "hello world!", "errors": { "device": [ "the selected device invalid" ] } } ] } }
first: fail response give doesn't validate. paste jsonlint , see fails validation. perhaps copy/paste error - need straightened out. aside, here steps.
- combine success , fail 1 json object (see example below)
- paste json website such http://json2csharp.com/. give classes model json object (seem second example below)
- uses newtonsoft's method, jsonconvert.deserializeobject(json) convert json string instance of rootobject class (rename class if like)
- user newly created class other, no longer needing aware came web service.
example 1: modified json
{ "success": true, "result": { "success": [ { "id": "308", "device_id": "4", "message": "hello world!", "status": "pending", "send_at": "1414624856", "queued_at": "0", "sent_at": "0", "delivered_at": "0", "expires_at": "1414634856", "canceled_at": "0", "failed_at": "0", "received_at": "0", "error": "none", "created_at": "1414624856", "contact": { "id": "14", "name": "phyllis turner", "number": "+447791064713" } } ], "fails": [ { "number": "+44771232343", "message": "hello world!", "device": 1, "errors": { "device": ["the selected device invalid"] } } ] } }
example 2 : list of classes needed
public class contact { public string id { get; set; } public string name { get; set; } public string number { get; set; } } public class success { public string id { get; set; } public string device_id { get; set; } public string message { get; set; } public string status { get; set; } public string send_at { get; set; } public string queued_at { get; set; } public string sent_at { get; set; } public string delivered_at { get; set; } public string expires_at { get; set; } public string canceled_at { get; set; } public string failed_at { get; set; } public string received_at { get; set; } public string error { get; set; } public string created_at { get; set; } public contact contact { get; set; } } public class errors { public list<string> device { get; set; } } public class fail { public string number { get; set; } public string message { get; set; } public int device { get; set; } public errors errors { get; set; } } public class result { public list<success> success { get; set; } public list<fail> fails { get; set; } } public class rootobject { public bool success { get; set; } public result result { get; set; } }
usage
static void main(string[] args) { var successjson = "{\"success\": true,\"result\": {\"success\": [{\"id\": \"308\",\"device_id\": \"4\"," + "\"message\": \"hello world!\",\"status\": \"pending\",\"send_at\": \"1414624856\", \"queued_at\": \"0\", " + "\"sent_at\": \"0\", \"delivered_at\": \"0\", \"expires_at\": \"1414634856\", \"canceled_at\": \"0\"," + "\"failed_at\": \"0\", \"received_at\": \"0\", \"error\": \"none\", \"created_at\": \"1414624856\"," + "\"contact\": {\"id\": \"14\", \"name\": \"phyllis turner\", \"number\": \"+447791064713\"} " + "}],\"fails\": []}}"; var successroot = jsonconvert.deserializeobject<rootobject>(successjson); var issuccess = successroot.result.success != null && successroot.result.success.count > 0; var isfail = successroot.result.fails != null && successroot.result.fails.count > 0; var message = successroot.result.success.first().message; }
Comments
Post a Comment