json.net

Iterating over JSON object in C#

核能气质少年 提交于 2019-12-27 17:01:24
问题 I am using JSON.NET in C# to parse a response from the Klout API. My response is like this: [ { "id": "5241585099662481339", "displayName": "Music", "name": "music", "slug": "music", "imageUrl": "http://kcdn3.klout.com/static/images/music-1333561300502.png" }, { "id": "6953585193220490118", "displayName": "Celebrities", "name": "celebrities", "slug": "celebrities", "imageUrl": "http://kcdn3.klout.com/static/images/topics/celebrities_b32741b6703151cc7bd85fba24c44c52.png" }, { "id":

JSON.NET as a WebAPI 2 OData serializer vs ODataMediaTypeFormatter

喜夏-厌秋 提交于 2019-12-27 14:57:59
问题 I'm trying to use JSON.NET as a default serializer in WebAPI 2 stack. I've implemented JsonMediaTypeFormatter, in which I've used JSON.NET serializer for serialize/deserialize data and created JsonContentNegotiator for using this media type formatter. All works fine except OData querying - if I add [Queryable] metadata ot action method, then response object doesn't contains any metadata information, only list of entities. Small example. My action method: [Queryable] public async Task

JSON.NET as a WebAPI 2 OData serializer vs ODataMediaTypeFormatter

最后都变了- 提交于 2019-12-27 14:56:26
问题 I'm trying to use JSON.NET as a default serializer in WebAPI 2 stack. I've implemented JsonMediaTypeFormatter, in which I've used JSON.NET serializer for serialize/deserialize data and created JsonContentNegotiator for using this media type formatter. All works fine except OData querying - if I add [Queryable] metadata ot action method, then response object doesn't contains any metadata information, only list of entities. Small example. My action method: [Queryable] public async Task

Json.Net: Serialize/Deserialize property as a value, not as an object

瘦欲@ 提交于 2019-12-27 10:58:02
问题 How can I achieve the following JSON representation of Id class when used in another class? class Car { public StringId Id { get; set; } public string Name { get; set; } } class StringId { public string Value { get; set; } } // --------------------------------------------- // Desired representation { "Id": "someId", "Name": "Ford" } // Default (undesired) representation { "Id" : { "Value": "someId" }, "Name": "Ford" } 回答1: You could add a TypeConverter for StringId . Json.NET will pick up the

Newtonsoft JSON Deserialize

北城余情 提交于 2019-12-27 10:53:09
问题 My JSON is as follows: {"t":"1339886","a":true,"data":[],"Type":[['Ants','Biz','Tro']]} I found the Newtonsoft JSON.NET deserialize library for C#. I tried to use it as follow: object JsonDe = JsonConvert.DeserializeObject(Json); How can I access to the JsonDe object to get all the "Type" Data? I tried it with a loop but it is not working because the object does not have an enumerator. 回答1: You can implement a class that holds the fields you have in your JSON class MyData { public string t;

Deserializing JSON into an object

我只是一个虾纸丫 提交于 2019-12-27 10:00:05
问题 I have some JSON: { "foo" : [ { "bar" : "baz" }, { "bar" : "qux" } ] } And I want to deserialize this into a collection. I have defined this class: public class Foo { public string bar { get; set; } } However, the following code does not work: JsonConvert.DeserializeObject<List<Foo>>(jsonString); How can I deserialize my JSON? 回答1: That JSON is not a Foo JSON array. The code JsonConvert.DeserializeObject<T>(jsonString) will parse the JSON string from the root on up , and your type T must

How to Deserialize a Json Array with C#?

不问归期 提交于 2019-12-27 06:16:16
问题 I use RestSharp to make a Rest API Call. public class GetValues { public string Values{ get; set; } } public class JsonObjects { public List<GetValues> Values{ get; set; } } RestRequest restRequest = new RestRequest("api/tempCatalog/temp1", Method.GET); restRequest.AddHeader("Accept", "application/json"); restRequest.AddHeader("Content-Type", "application/json"); restRequest.AddHeader("Authorization", token); IRestResponse restResponse = clientRest.Execute(restRequest ); string Content =

How to Deserialize a Json Array with C#?

余生长醉 提交于 2019-12-27 06:14:04
问题 I use RestSharp to make a Rest API Call. public class GetValues { public string Values{ get; set; } } public class JsonObjects { public List<GetValues> Values{ get; set; } } RestRequest restRequest = new RestRequest("api/tempCatalog/temp1", Method.GET); restRequest.AddHeader("Accept", "application/json"); restRequest.AddHeader("Content-Type", "application/json"); restRequest.AddHeader("Authorization", token); IRestResponse restResponse = clientRest.Execute(restRequest ); string Content =

Json does not contain a definiton for 'stringify'?

感情迁移 提交于 2019-12-25 19:13:11
问题 I am trying to update a JIRA issue using Visual Studio 2015, Microsoft Razor, and C# with Json.Net 10.0.2. The code is: public String updateJiraIssue(object objJira2) { JiraService.open("PUT", JiraUrl + "/rest/api/2/issue/NPI-24"); JiraService.setRequestHeader("Content-Type", "application/json"); JiraService.setRequestHeader("Accept", "application/json"); JiraService.setRequestHeader("X-Atlassian-Token", "nocheck"); JiraService.setRequestHeader("Authorization", "Basic " +

How to add “undefined” to a JObject collection - where is JToken/JValue.Undefined?

大兔子大兔子 提交于 2019-12-25 18:55:23
问题 When using Json.NET, I am trying to create a JSON structure dynamically using the "JSON to LINQ" support. In the following, jObject is a JObject and JObject.Add takes (string, JToken). However, I cannot find out how to add either an Undefined or a Null token - nor can I find out how to create JValues with the appropriate Null/Undefined type. string value = GetValue(); if (value == "undefined") { jObject.Add(key, /* how to add "Undefined" token? */); } else if (value == "null") { jObject.Add