json.net

How to add a root node to a JSON in C# using Json.NET?

血红的双手。 提交于 2020-04-14 06:04:06
问题 I am working on Visual Studio C# project and I need to convert a JSON to XML . I receive the JSON in string format. The problem is, I need to have a root node in the JSON structure if the JSON doesn't have one, so that I can convert to XML with desired format. Supose I have this JSON : { "id": 1, "name": { "first": "Yong", "last": "Mook Kim" }, "contact": [{ "type": "phone/home", "ref": "111-111-1234" }, { "type": "phone/work", "ref": "222-222-2222" }] } And I want to add root node to that

How to add a root node to a JSON in C# using Json.NET?

泄露秘密 提交于 2020-04-14 06:02:50
问题 I am working on Visual Studio C# project and I need to convert a JSON to XML . I receive the JSON in string format. The problem is, I need to have a root node in the JSON structure if the JSON doesn't have one, so that I can convert to XML with desired format. Supose I have this JSON : { "id": 1, "name": { "first": "Yong", "last": "Mook Kim" }, "contact": [{ "type": "phone/home", "ref": "111-111-1234" }, { "type": "phone/work", "ref": "222-222-2222" }] } And I want to add root node to that

Json.NET deserializing contents of a JObject?

家住魔仙堡 提交于 2020-04-13 20:40:48
问题 If I have a JObject , which contains the property "Fields". How do I pull out the contents of this property to an object[] with deserialized elements? It seems like no matter what I do, I only get arrays of other JObjects. myJObject["Fields"] { "$type": "System.Object[], mscorlib", "$values": [ 123, "hello" ] } In this case, I want to get an object array containing a long 123 and the string "hello" . 回答1: Use ToObject(): var array = myJObject["Fields"].ToObject<object[]>(); Debug.Assert(array

Illegal character in property name when serializing JSON to dynamic

一世执手 提交于 2020-04-13 06:35:13
问题 I am trying to deserialize JSON that contains dash (-) characters in some of its property names, by using dynamic types: string json = MyWebClient.DownloadString("http://api.crossref.org/works/10.1093/brain/75.2.187"); dynamic result = JsonConvert.DeserializeObject<dynamic>(json); string title = result.message.title[0]; string journal = result.message.container-title[0]; I cannot get the "container-title" value due to use of an illegal character. And I don't want to simply use Replace() to

Illegal character in property name when serializing JSON to dynamic

你离开我真会死。 提交于 2020-04-13 06:35:07
问题 I am trying to deserialize JSON that contains dash (-) characters in some of its property names, by using dynamic types: string json = MyWebClient.DownloadString("http://api.crossref.org/works/10.1093/brain/75.2.187"); dynamic result = JsonConvert.DeserializeObject<dynamic>(json); string title = result.message.title[0]; string journal = result.message.container-title[0]; I cannot get the "container-title" value due to use of an illegal character. And I don't want to simply use Replace() to

Deserialize JSON array with different types

倾然丶 夕夏残阳落幕 提交于 2020-04-13 06:15:13
问题 I'm new to JSON.NET, and I need help to deserialize the following JSON { "items": [ [10, "file1", "command 1"], [20, "file2", "command 2"], [30, "file3", "command 3"] ] } to this IList<Item> Items {get; set;} class Item { public int Id {get; set} public string File {get; set} public string Command {get; set} } The content in the JSON is always in the same order. 回答1: You can use a custom JsonConverter to convert each child array in the JSON to an Item . Here is the code you would need for the

Code-Serialization:System.Web.Serialization.JavaScriptSerializer.cs

假装没事ソ 提交于 2020-04-12 16:09:22
ylbtech-Code-Serialization:System.Web.Serialization.JavaScriptSerializer.cs 1. 返回顶部 1、 using System; using System.Collections.Generic; using System.Web.Script.Serialization; // 引用序列化类库 public partial class _Default : System.Web.UI.Page { /// <summary> /// 应对 Json.NET 使用序列化和反序列化。 为启用 AJAX 的应用程序提供序列化和反序列化功能。 /// ByYlbtech /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Page_Load( object sender, EventArgs e) { var RegisteredUsers = new List<Person> (); RegisteredUsers.Add( new Person() { PersonID = 1 , Name = " Bryon Hetrick " , Registered = true });

Serialize to JSON with parenthesis in property name

梦想的初衷 提交于 2020-04-07 06:15:10
问题 I'm using paypal api to do some payment stuff. If I look at SetExpressCheckout some field are in the form PAYMENTREQUEST_n_AMT . That's fine for me because I has a request class like this: public class SetExpressCheckoutRequest { public string PAYMENTREQUEST_0_AMT { get; set; } } That works. Now I need to use the PAY operation which has fields like receiverList.receiver(0).email Since parenthesis are not allowed in c# property names, how am I supposed to write a corresponding property on my

Serialize to JSON with parenthesis in property name

青春壹個敷衍的年華 提交于 2020-04-07 06:13:09
问题 I'm using paypal api to do some payment stuff. If I look at SetExpressCheckout some field are in the form PAYMENTREQUEST_n_AMT . That's fine for me because I has a request class like this: public class SetExpressCheckoutRequest { public string PAYMENTREQUEST_0_AMT { get; set; } } That works. Now I need to use the PAY operation which has fields like receiverList.receiver(0).email Since parenthesis are not allowed in c# property names, how am I supposed to write a corresponding property on my

Deserialize JSON-File with multiple datatypes for a key

北慕城南 提交于 2020-03-24 02:56:12
问题 I wanted to analyze Telegram-Chats so I exported a chat in JSON format and wanted to deserialize it into my analyzing software. { "id": 397910, "type": "message", "date": "2018-02-21T10:27:59", "edited": "1970-01-01T01:00:00", "from": "Username", "from_id": 39033284, "text": "Some Text" } So I've used this simple code to read the JSON List<JSONObject> jsonObjects = JsonConvert.DeserializeObject<List<JSONObject>>(File.ReadAllText(openFileDialog.FileName)); public class JSONObject { public int