system.text.json

Adding a property into specified location into json using Newtonsoft.Json

半世苍凉 提交于 2020-06-07 07:24:29
问题 I have a following JSON . I have added 2 x double quotes so that I could declare it as string variable in c# , in reality it contains one times double quotes for properties. The complete example code is in this fiddle. I am using Newtonsoft but can use System.Text.Json as well so a working solution for either will be appreciated. { ""display"": ""wizard"", ""settings"": {}, ""components"": [ { ""title"": ""Activity Information"", ""label"": ""Activity Information"", ""type"": ""Activity"", "

Adding a property into specified location into json using Newtonsoft.Json

拜拜、爱过 提交于 2020-06-07 07:24:29
问题 I have a following JSON . I have added 2 x double quotes so that I could declare it as string variable in c# , in reality it contains one times double quotes for properties. The complete example code is in this fiddle. I am using Newtonsoft but can use System.Text.Json as well so a working solution for either will be appreciated. { ""display"": ""wizard"", ""settings"": {}, ""components"": [ { ""title"": ""Activity Information"", ""label"": ""Activity Information"", ""type"": ""Activity"", "

.net Core 3 [JsonIgnore] not working when requesting single resource

独自空忆成欢 提交于 2020-05-31 05:22:51
问题 in my .net Core 3.0 Api the [JsonIgnore] Attribute is not working as excepted. Im using System.Text.Json instead of the old Newtonsoft.Json When i'm using my resource that returns a list of Objects, for example: /api/Object/ the objects are serialized like this: [ { "id": 1, "date": "2020-02-12T08:45:51.502", "userId": 1, "tags": [ { "name": "string" } ] } ] But when I request a single result /api/Object/{id} the full object gets serialized like this: { "user": { "hasAccess": false, "id": 1,

Deserialize JSON string embedded within JSON directly

拥有回忆 提交于 2020-05-16 04:07:11
问题 I'm using .netcore 3.1 and I'm using System.Text.Json for serialization and deserialization. I didn't know how to phrase my question precisely. I looked around but couldn't find a direct answer for my question. Apologies if it's a duplicate. This is a sample JSON response. { "properties": { "subscriptionId": "sub1", "usageStartTime": "2015-03-03T00:00:00+00:00", "usageEndTime": "2015-03-04T00:00:00+00:00", "instanceData": "{\"Microsoft.Resources\":{\"resourceUri\":\"resourceUri1\",\"location\

Deserialize JSON string embedded within JSON directly

吃可爱长大的小学妹 提交于 2020-05-16 04:05:11
问题 I'm using .netcore 3.1 and I'm using System.Text.Json for serialization and deserialization. I didn't know how to phrase my question precisely. I looked around but couldn't find a direct answer for my question. Apologies if it's a duplicate. This is a sample JSON response. { "properties": { "subscriptionId": "sub1", "usageStartTime": "2015-03-03T00:00:00+00:00", "usageEndTime": "2015-03-04T00:00:00+00:00", "instanceData": "{\"Microsoft.Resources\":{\"resourceUri\":\"resourceUri1\",\"location\

How to pretty-print (format) System.Text.JsonElement to a string

人盡茶涼 提交于 2020-05-15 04:17:51
问题 Where is the API to format an existing JsonElement to a formatted JSON string. The ToString() API does not provide any formatting options. Falling back to using Newtonsoft is pretty annoying Newtonsoft.Json.Linq.JValue .Parse(myJsonElement.GetRawText()) .ToString(Newtonsoft.Json.Formatting.Indented) 回答1: You can re-serialize your JsonElement with JsonSerializer and set JsonSerializerOptions.WriteIndented = true, e.g. in an extension method: public static partial class JsonExtensions { public

How to pretty-print (format) System.Text.JsonElement to a string

荒凉一梦 提交于 2020-05-15 04:17:09
问题 Where is the API to format an existing JsonElement to a formatted JSON string. The ToString() API does not provide any formatting options. Falling back to using Newtonsoft is pretty annoying Newtonsoft.Json.Linq.JValue .Parse(myJsonElement.GetRawText()) .ToString(Newtonsoft.Json.Formatting.Indented) 回答1: You can re-serialize your JsonElement with JsonSerializer and set JsonSerializerOptions.WriteIndented = true, e.g. in an extension method: public static partial class JsonExtensions { public

System.Text.Json Deserialize nested object from API call - Data is wrapped in parent JSON property

孤街醉人 提交于 2020-05-09 07:03:24
问题 I have an API JSON response that wraps the data content in a data property, which looks like this: { "data":{ "email":"admin@example.com", "mobile":"+1555555123", "id":4, "first_name":"Merchant", "last_name":"Vendor", "role":"Merchant", } } So when making request for a User Object with a Library like RequestSharp , the response.Content has the content for the User wrapped in the data json property as it comes from the API. Code: var request = RequestHelper.CreateTokenRequest(email, password);

System.Text.JSON doesn't deserialize what Newtonsoft does

只谈情不闲聊 提交于 2020-05-09 05:27:57
问题 I have a json that the new System.Text.Json.JsonSerializer.Deserialize<T>(json_data) serialize as List<T> with the correct numbers of elements, but then the objects inside have all the values null or 0 Same json with Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json_data) is correctly filled. Class: public class SensorValue { public string StationCode { get; set; } public string SensorCode { get; set; } public string SensorNameIt { get; set; } public string SensorNameDe { get; set; }

System.Text.Json - Deserialize nested object as string

感情迁移 提交于 2020-04-30 05:12:00
问题 I'm trying to use the System.Text.Json.JsonSerializer to deserialize the model partially, so one of the properties is read as string that contains the original JSON. public class SomeModel { public int Id { get; set; } public string Name { get; set; } public string Info { get; set; } } The example code var json = @"{ ""Id"": 1, ""Name"": ""Some Name"", ""Info"": { ""Additional"": ""Fields"", ""Are"": ""Inside"" } }"; var model = JsonSerializer.Deserialize<SomeModel>(json); should produce the