json.net

Newtonsoft Object → Get JSON string

放肆的年华 提交于 2019-12-20 16:19:25
问题 I have an object that is created by Newtonsoft's JSON serializer. I need to get the JSON string that was used to create the object. How do I serialize the object into a simple JSON string? 回答1: Try this: public string jsonOut() { // Returns JSON string. return JsonConvert.SerializeObject(this); } 来源: https://stackoverflow.com/questions/6690395/newtonsoft-object-%e2%86%92-get-json-string

Newtonsoft Object → Get JSON string

陌路散爱 提交于 2019-12-20 16:18:06
问题 I have an object that is created by Newtonsoft's JSON serializer. I need to get the JSON string that was used to create the object. How do I serialize the object into a simple JSON string? 回答1: Try this: public string jsonOut() { // Returns JSON string. return JsonConvert.SerializeObject(this); } 来源: https://stackoverflow.com/questions/6690395/newtonsoft-object-%e2%86%92-get-json-string

.Net Linq to JSON with Newtonsoft JSON library

╄→尐↘猪︶ㄣ 提交于 2019-12-20 15:09:10
问题 I have some JSON that is sent to my webservice that looks something like this. { root: [ { "key": "foo1", "val": "150" }, { "key": "foo2", "val": "220" }, { "key": "foo3", "val": "300" }, { "key": "dataid", "val": "2289" } ] } Say I wanted to return the value of val where key is equal to "dataid" . How would I do this using the JSON.Net library? I know I can loop through the values to find it but it is likely that the object will be far bigger than this example here. Thanks in advance 回答1:

JSON.NET JObject - how do I get value from this nested JSON structure

淺唱寂寞╮ 提交于 2019-12-20 12:31:24
问题 I have this JSON: { "client_id": "26075235", "client_version": "1.0.0", "event": "app.uninstall", "timestamp": 1478741247, "data": { "user_id": "62581379", "site_id": "837771289247593785", "platform_app_id": "26075235" } } I parse it into a JSON.NET JObject and I can successfully access the first level of values using e.g. (string)RequestBody.SelectToken("client_id") How do I access the value of "user_id" using a JPath expression (or by accessing a child object of the JSON.NET JObject)? This

How to tell JSON.NET StringEnumConverter to take DisplayName?

不羁岁月 提交于 2019-12-20 10:27:10
问题 I've got the following model: public enum Status { [Display(Name = "Awaiting Approval")] AwaitingApproval, Rejected, Accepted, } I use this enum in a model like this: public class Docs { [Key] public int Id { get; set; } [JsonConverter(typeof(StringEnumConverter))] public Status Status { get; set; } } Now this works fine; the serializer returns the string equivalent of the enum. My question is how to tell JSON.NET to take the Display attribute instead of the string ? 回答1: You should try using

Custom JsonConverter WriteJson Does Not Alter Serialization of Sub-properties

末鹿安然 提交于 2019-12-20 10:22:37
问题 I always had the impression that the JSON serializer actually traverses your entire object's tree, and executes the custom JsonConverter's WriteJson function on each interface-typed object that it comes across - not so. I have the following classes and interfaces: public interface IAnimal { string Name { get; set; } string Speak(); List<IAnimal> Children { get; set; } } public class Cat : IAnimal { public string Name { get; set; } public List<IAnimal> Children { get; set; } public Cat() {

Custom JsonConverter WriteJson Does Not Alter Serialization of Sub-properties

时光总嘲笑我的痴心妄想 提交于 2019-12-20 10:21:14
问题 I always had the impression that the JSON serializer actually traverses your entire object's tree, and executes the custom JsonConverter's WriteJson function on each interface-typed object that it comes across - not so. I have the following classes and interfaces: public interface IAnimal { string Name { get; set; } string Speak(); List<IAnimal> Children { get; set; } } public class Cat : IAnimal { public string Name { get; set; } public List<IAnimal> Children { get; set; } public Cat() {

Run default serialization logic from JsonConverter

て烟熏妆下的殇ゞ 提交于 2019-12-20 10:18:35
问题 I have a JsonConverter that, depending on an instance specific flag, needs to either run custom serialization logic run the default Json.NET serialization logic How can the default Json.NET serialization logic be ran from a JsonConverter ? Thanks 回答1: Here is an example. Say your class to serialize looks like this: class Foo { public bool IsSpecial { get; set; } public string A { get; set; } public string B { get; set; } public string C { get; set; } } The IsSpecial flag is used to control

How to create my json string by using C#?

旧巷老猫 提交于 2019-12-20 09:45:41
问题 I am new to json format. I want to create a following json string by using C# and json.net package. This is my target Json format: { "GetQuestions": { "s1":"Q1,Q2", "s2":"Q1,Q3", "s3":"Q4,Q5" } } Here, i am storing each students questions.But sometimes, the total number of students may be vary.for example it may be s1,s2 only or s1,s2,s3,s4.... This is calculated at runtime in C#.So, i want to create the json string depending on the student list.... Please guide me to get out of this issue?

How to serialize a JObject without the formatting?

假如想象 提交于 2019-12-20 09:25:50
问题 I have a JObject (I'm using Json.Net) that I constructed with LINQ to JSON (also provided by the same library). When I call the ToString() method on the JObject , it outputs the results as formatted JSON. How do I set the formatting to "none" for this? 回答1: Call JObject's ToString(Formatting.None) method. Alternatively if you pass the object to the JsonConvert.SerializeObject method it will return the JSON without formatting. Documentation: Write JSON text with JToken.ToString 回答2: You can