json.net

Deserialize dynamic object name for newtonsoft property

☆樱花仙子☆ 提交于 2019-12-11 11:52:02
问题 When I am sending request for a certain API, they return me a json which is awesome, but the problem is that depending on the parameters I provide, the object name is always different while the data structure remains the same. So I am trying to convert the json to a C# class using Newtonsoft library. The only way I've found to do this is by using JsonTextReader, but I'm wondering if there is a cleaner way of achieving this, I looked up the documentation and couldn't find anything to help me

JSON.net CF 3.5, verify that JSON is complete

佐手、 提交于 2019-12-11 11:42:20
问题 I'm using Json.net in a 3.5 CF setting and have a problem verifying that a string is indeed complete JSON. I am using: var o = JObject.Parse(incomingString); which will return null if the JSON is incomplete - but not always. If the JSON is something "mostly formed", it will parse correctly. This simple example returns an object: { "Name":"Bob", "Pets":[ {"Type":"Cat", "Name":"Pudge" but if I break the JSON elsewhere, it returns null as expected. { "Name":"Bob", "Pets":[ {"Type":"Cat", "Nam

Json.Net duplicates private list items

可紊 提交于 2019-12-11 11:39:08
问题 Currently I'm trying to utilize Json.Net serializer in my projects. One of the problems that I've faced recentry is that deserialized object gets all of it's private list items duplicated. It can be reproduced like this: Create class with private field of List<> type, where List<> generic parameter is some other class Add public property, that gets/sets private field Create instance of that class, add 1 item to inner list, using property. Serialize with new DefaultContractResolver that is

JSON .Net Unity Deserialize dictionary of anonymous objects

大城市里の小女人 提交于 2019-12-11 11:15:15
问题 I have a Dictionary that I serialize onto a binary file, and deserialize back again using JSON .net from https://json.codeplex.com/ The dictionary may contain abritrary objects (string, classes, even List). Each class is [System.Serializable] At serialize time, I add serializer.TypeNameHandling = TypeNameHandling.All; to make sure the deserializer has the type info required to deserialize the dictionary. I am unable to deserialize it correctly to the identical list of object, I only get

Does the new `System.Text.Json` have a required property attribute?

若如初见. 提交于 2019-12-11 10:56:15
问题 I've combed through the MS docs but cannot find an attribute equivalent to the NewtonSoft JsonPropertyRequired. What I'm looking for this: public class Videogame { [JsonProperty(Required = Required.Always)] public string Name { get; set; } } Am I just missing something or does this level of validation not exist in the Microsoft library? 回答1: Not as of .NET core 3.0. The only ones supported are: JsonConverterAttribute JsonExtensionDataAttribute JsonIgnoreAttribute JsonPropertyNameAttribute

Camel Cased JSON.Net Deserialization from Web API Request

流过昼夜 提交于 2019-12-11 10:35:16
问题 I've recently upgraded my project to use JSON.Net version 7.0.1 (was previously using version 6.0.3) and I've run into an issue where my models are no longer being populated with the values from the camel cased JSON source in the http response. Here's an example of the model I'm using: public class FooModel { public string Bar { get; set; } public int Id { get; set; } } In this app, I'm creating my http request on the server side and requesting content from a built-in web api controller. My

How can i Generate Json using jsonconvert.serializeobject?

我是研究僧i 提交于 2019-12-11 10:33:52
问题 I need to generate json like follows using Class and Objects in C# . { "title": "Joseph Sagayam", "url": "#person-Joseph", "description": "DBA", "actions": [ { "icon": "fa-user-plus", "url": "#add-contact" }, { "icon": "fa-comment-o", "url": "#message-contact" }, { "icon": "fa-birthday-cake", "url": "#birthday" } ] } and also if i need to add blow json value { "people": { "categoryName": "People", "results": [ { "title": "Joseph Sagayam", "url": "#person-Joseph", "description": "DBA",

Deserializing JSON in Windows Phone 7 with Json.NET

扶醉桌前 提交于 2019-12-11 10:19:35
问题 I know that there are countless questions about this. I've read many of them, but to little understanding. Can you help clarify the process of deserializing JSON in WP7? I've got JSON that looks like this: { "status" : { "code" : 99 , "message" : "Already checked in" }, "response" : { "token" : "faisdufhdaisuflasdkf", "distance" : 20, "angle" : 45 } } I am trying to use Json.NET, but this is where my understanding is pretty much naught. var deserializedJSON = JsonConvert.DeserializeObject

Unexpected character encountered while parsing value:

被刻印的时光 ゝ 提交于 2019-12-11 10:13:42
问题 I'm trying to use Steam's Web API to receive JSON and parse it using JSON.Net. I'm simply hard-coding a URL where I know I'll receive JSON. I'm running into the following error when I run it though: Unexpected character encountered while parsing value: . Path '', line 0, position 0. The error points to line 22 of my controller: Line 22: SteamResponse response = JsonConvert.DeserializeObject(json); Here are my classes: public class Game { public int appid { get; set; } public int playtime

Deserialize object using JSON.NET without the need for a container

妖精的绣舞 提交于 2019-12-11 09:57:37
问题 I am using JSON.NET to deseralize JSON strings that I receive from a service to my business objects. I have a nice Service pattern that parses all my JSON strings from a given REST URL to an object as follows: private async Task<T> LoadJSONToObject<T>(string url) { //get data var json = await GetResultStringAsync(url); //deserialize it var results = JsonConvert.DeserializeObject<T>(json); return results; } The challenge that I am having is how do I use the above pattern with collections