Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Programming Q&A
C#/PHP read JSON string from PHP into C#
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Skythrust" data-source="post: 456461" data-attributes="member: 87030"><p>Thanks griimnak, but the code is messy because I need to get some data out of SQLSRV <img src="/styles/default/xenforo/smilies/emojione/smile.png" class="smilie" loading="lazy" alt=":)" title="Smile :)" data-shortname=":)" /></p><p>[automerge]1581195764[/automerge]</p><p>Okay Imma bit further.. I can move on now without any errors.</p><p></p><p>But my messagebox will be empty when I ask a record up..</p><p></p><p>[CODE=csharp] public ObjectTypeList data { get; set; }</p><p></p><p> public class ObjectTypeList</p><p> {</p><p> [JsonProperty(PropertyName = "info")]</p><p> public Dictionary<string, Object> info { get; set; }</p><p> }</p><p></p><p> public class Object</p><p> {</p><p> [JsonProperty(PropertyName = "Firstname")]</p><p> public string Firstname { get; set; }</p><p> [JsonProperty(PropertyName = "Middlename")]</p><p> public string Middlename { get; set; }</p><p> [JsonProperty(PropertyName = "Lastname")]</p><p> public string Lastname { get; set; }</p><p> [JsonProperty(PropertyName = "Badge")]</p><p> public int CredentialNumber { get; set; }</p><p> [JsonProperty(PropertyName = "CustomerId")]</p><p> public string CustomerId { get; set; }</p><p> }</p><p> </p><p> using (var webClient = new WebClient())</p><p> {</p><p> string json = webClient.DownloadString("http://localhost/intergrator/test.php?uid=U569418&pwd=P497965");</p><p> Object Personal = JsonConvert.DeserializeObject<Object>(json);</p><p> MessageBox.Show("JSON result: " + Personal.Firstname);</p><p> }[/CODE]</p><p>[automerge]1581256740[/automerge]</p><p>Solved.</p><p></p><p>[CODE=csharp]</p><p>public class Object</p><p> {</p><p> public string Firstname { get; set; }</p><p> public string Middlename { get; set; }</p><p> public string Lastname { get; set; }</p><p> public string CredentialNumber { get; set; }</p><p> public string CustomerId { get; set; }</p><p> }</p><p></p><p>void JSONtest()</p><p> {</p><p> using (var webClient = new WebClient())</p><p> {</p><p> string json = webClient.DownloadString("URL");</p><p> var Personal = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Object>>(json);</p><p></p><p> foreach (var pv in Personal)</p><p> {</p><p> // Do something </p><p> // example MessageBox.Show(ev.Firstname);</p><p> }</p><p> }</p><p> }</p><p>[/CODE]</p><p>[automerge]1589794677[/automerge]</p><p></p><p></p><p>I would like to use this code in a service, but I am not able to do that</p><p>[CODE]</p><p> using (var webClient = new WebClient())</p><p> {</p><p> string json = webClient.DownloadString(new Uri("http://localhost/test.php"));</p><p> var Personal = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Object>>(json);</p><p></p><p> foreach (var pv in Personal)</p><p> {</p><p> string Badge = pv.CredentialNumber;</p><p> string Firstname = pv.Firstname;</p><p> string Middlename = pv.Middlename;</p><p> string Lastname = pv.Lastname;</p><p></p><p> SqlConnection sc = new SqlConnection(GetConnectionStringTemp());</p><p> {</p><p> sc.Open();</p><p></p><p> string query = string.Format("INSERT INTO Visitors (Badge,Firstname,Middlename,Lastname) VALUES ('{0}','{1}','{2}','{3}')"</p><p></p><p> , Badge //0</p><p> , Firstname //1</p><p> , Middlename //2</p><p> , Lastname //3</p><p> );</p><p></p><p> using (SqlCommand command = new SqlCommand(query, sc))</p><p> {</p><p> int countrecords = command.ExecuteNonQuery();</p><p> }</p><p> sc.Close();</p><p> }</p><p> }</p><p> }</p><p>[/CODE]</p><p></p><p>when I paste this code in a Winform I am able to recieve the information, and I am able to insert it into a SQLSRV.</p></blockquote><p></p>
[QUOTE="Skythrust, post: 456461, member: 87030"] Thanks griimnak, but the code is messy because I need to get some data out of SQLSRV :-) [automerge]1581195764[/automerge] Okay Imma bit further.. I can move on now without any errors. But my messagebox will be empty when I ask a record up.. [CODE=csharp] public ObjectTypeList data { get; set; } public class ObjectTypeList { [JsonProperty(PropertyName = "info")] public Dictionary<string, Object> info { get; set; } } public class Object { [JsonProperty(PropertyName = "Firstname")] public string Firstname { get; set; } [JsonProperty(PropertyName = "Middlename")] public string Middlename { get; set; } [JsonProperty(PropertyName = "Lastname")] public string Lastname { get; set; } [JsonProperty(PropertyName = "Badge")] public int CredentialNumber { get; set; } [JsonProperty(PropertyName = "CustomerId")] public string CustomerId { get; set; } } using (var webClient = new WebClient()) { string json = webClient.DownloadString("http://localhost/intergrator/test.php?uid=U569418&pwd=P497965"); Object Personal = JsonConvert.DeserializeObject<Object>(json); MessageBox.Show("JSON result: " + Personal.Firstname); }[/CODE] [automerge]1581256740[/automerge] Solved. [CODE=csharp] public class Object { public string Firstname { get; set; } public string Middlename { get; set; } public string Lastname { get; set; } public string CredentialNumber { get; set; } public string CustomerId { get; set; } } void JSONtest() { using (var webClient = new WebClient()) { string json = webClient.DownloadString("URL"); var Personal = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Object>>(json); foreach (var pv in Personal) { // Do something // example MessageBox.Show(ev.Firstname); } } } [/CODE] [automerge]1589794677[/automerge] I would like to use this code in a service, but I am not able to do that [CODE] using (var webClient = new WebClient()) { string json = webClient.DownloadString(new Uri("http://localhost/test.php")); var Personal = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Object>>(json); foreach (var pv in Personal) { string Badge = pv.CredentialNumber; string Firstname = pv.Firstname; string Middlename = pv.Middlename; string Lastname = pv.Lastname; SqlConnection sc = new SqlConnection(GetConnectionStringTemp()); { sc.Open(); string query = string.Format("INSERT INTO Visitors (Badge,Firstname,Middlename,Lastname) VALUES ('{0}','{1}','{2}','{3}')" , Badge //0 , Firstname //1 , Middlename //2 , Lastname //3 ); using (SqlCommand command = new SqlCommand(query, sc)) { int countrecords = command.ExecuteNonQuery(); } sc.Close(); } } } [/CODE] when I paste this code in a Winform I am able to recieve the information, and I am able to insert it into a SQLSRV. [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Programming Q&A
C#/PHP read JSON string from PHP into C#
Top