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: 456442" data-attributes="member: 87030"><p>Herewith the code of the php page; <a href="https://codepen.io/Permudious/pen/OJVPRZZ" target="_blank">https://codepen.io/Permudious/pen/OJVPRZZ</a></p><p></p><p>Output PHP: [{"Firstname":"Janet","Middlename":"van","Lastname":"Hope","CredentialNumber":"12018","CustomerId":5}]</p><p>Error in C#</p><p></p><p>Newtonsoft.Json.JsonSerializationException</p><p> HResult=0x80131500</p><p> Message=Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,System.String]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.</p><p>To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.</p><p>Path '', line 2, position 1.</p><p> Source=Newtonsoft.Json</p><p> StackTrace:</p><p> at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureArrayContract(JsonReader reader, Type objectType, JsonContract contract)</p><p> at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)</p><p> at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)</p><p> at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)</p><p> at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)</p><p> at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)</p><p> at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)</p><p> at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)</p><p> at CA4000_EV_Configuration_Tool.Form1.JSONtest() in testl\Form1.cs:line 281</p><p> at CA4000_EV_Configuration_Tool.Form1.Form1_Load(Object sender, EventArgs e) in test\Form1.cs:line 480</p><p> at System.Windows.Forms.Form.OnLoad(EventArgs e)</p><p> at System.Windows.Forms.Form.OnCreateControl()</p><p> at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)</p><p> at System.Windows.Forms.Control.CreateControl()</p><p> at System.Windows.Forms.Control.WmShowWindow(Message& m)</p><p> at System.Windows.Forms.Control.WndProc(Message& m)</p><p> at System.Windows.Forms.ScrollableControl.WndProc(Message& m)</p><p> at System.Windows.Forms.Form.WmShowWindow(Message& m)</p><p> at System.Windows.Forms.Form.WndProc(Message& m)</p><p> at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)</p><p> at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)</p><p> at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)</p><p>[automerge]1581109278[/automerge]</p><p>Update; just add this code above the void to test;</p><p></p><p>[CODE=csharp] public ObjectTypeList data { get; set; }</p><p></p><p> public class ObjectTypeList</p><p> {</p><p> public Dictionary<string, Object> objects { get; set; }</p><p> }</p><p></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> }[/CODE]</p></blockquote><p></p>
[QUOTE="Skythrust, post: 456442, member: 87030"] Herewith the code of the php page; [URL]https://codepen.io/Permudious/pen/OJVPRZZ[/URL] Output PHP: [{"Firstname":"Janet","Middlename":"van","Lastname":"Hope","CredentialNumber":"12018","CustomerId":5}] Error in C# Newtonsoft.Json.JsonSerializationException HResult=0x80131500 Message=Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,System.String]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '', line 2, position 1. Source=Newtonsoft.Json StackTrace: at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureArrayContract(JsonReader reader, Type objectType, JsonContract contract) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value) at CA4000_EV_Configuration_Tool.Form1.JSONtest() in testl\Form1.cs:line 281 at CA4000_EV_Configuration_Tool.Form1.Form1_Load(Object sender, EventArgs e) in test\Form1.cs:line 480 at System.Windows.Forms.Form.OnLoad(EventArgs e) at System.Windows.Forms.Form.OnCreateControl() at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible) at System.Windows.Forms.Control.CreateControl() at System.Windows.Forms.Control.WmShowWindow(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ScrollableControl.WndProc(Message& m) at System.Windows.Forms.Form.WmShowWindow(Message& m) at System.Windows.Forms.Form.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) [automerge]1581109278[/automerge] Update; just add this code above the void to test; [CODE=csharp] public ObjectTypeList data { get; set; } public class ObjectTypeList { public Dictionary<string, Object> objects { get; set; } } 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; } }[/CODE] [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Programming Q&A
C#/PHP read JSON string from PHP into C#
Top