jsonserializer

How to serialize class that derives from class decorated with DataContract(IsReference=true)?

筅森魡賤 提交于 2020-01-04 05:13:52
问题 I have class A that derives from System.Data.Objects.DataClasses.EntityObject . When I try to serialize using var a = new A(); DataContractJsonSerializer serializer = new DataContractJsonSerializer(a.GetType()); serializer.WriteObject(Response.OutputStream, a); I get error TestController+A._Id' is not marked with OptionalFieldAttribute, thus indicating that it must be serialized. However, 'TestController+A' derives from a class marked with DataContractAttribute and an IsReference setting of

Serialize JSON string that contains escaped (backslash and double quote) Swift return Badly formed object

本小妞迷上赌 提交于 2020-01-01 12:01:39
问题 I have response string from the backend like this: { "status": "success", "data": "{\"name\":\"asd\",\"address\":\"Street 1st\"}" } I think the problem was because the double quote (") in the data String. When I remove the double quote, the serialization was success. But the response is from backend and I have to deal with it. Anyone can solve this problem? Thank you. Here is the playground code. import Foundation var jsonStr = """ { "status": "success", "data": "{\"name\":\"asd\",\"address\"

Json.net failing to load certain properties belonging to a class object?

二次信任 提交于 2019-12-31 07:24:46
问题 NOTE: I'm adding this community wiki entry to save someone the loss of time I just went through debugging this problem. I have a class object with multiple public properties. I can serialize it fine using JSON.net. But when I load the JSON text back and deserialize it using JsonConvert.DeserializeObject<> , some of the fields are set to NULL when they definitely had valid values at the time of serialization. I inspected the serialized JSON string manually and I definitely see values for the

How to do Conditional Serialization using C# - NewtonSoft.Json

时光总嘲笑我的痴心妄想 提交于 2019-12-30 13:42:28
问题 I am doing json serialization using NewtonSoft.Json public class CommonBase { [JsonProperty(PropertyName = "u_customer_id")] public long CustomerId { get; set; } } I want to do a conditional serialization so that if CustomerId value is 0, I want to set a blank value for CustomerId during json serialization. Since CommonBase is a base class and I am not able to change data type from long to string . How can I achieve this? 回答1: You almost have the answer in your question title. What you are

How to map JSON response to custom class object

佐手、 提交于 2019-12-30 05:11:13
问题 I am calling an API in C# using unirest.io. I get following JSON response (as response.Body ). { "persons": [{ "id": "a010", "name": "Joe", "subjects": [ "Math", "English" ] }, { "id": "b020", "name": "Jill", "subjects": [ "Science", "Arts" ] }] } I tried to map this to my custom class object as follows. HttpRequest request = Unirest.get(API_V1_URL).header("accept", "application/json"); HttpResponse<string> response = request.asString(); var serializer = new JavaScriptSerializer(); persons =

Convert string to Json Array

我的梦境 提交于 2019-12-24 12:35:03
问题 I have a Dictionary like that: Dictionary<string, string> // key value pair and the example values are: Dictionary<string, string> parameters = new Dictionary<string, string>(); parameters.Add("key1","value1"); parameters.Add("products","[{"name":"Widget A","qty":3},{"name":"Widget B","qty":1}]"); how Can I serialize that as : vars={"key1":"value1",products":[{"name":"Widget A","qty":3},{"name":"Widget B","qty":1}]} When I try to serialize : vars={"key1":"value1",products":**"**[{"name":

How to get query string from JavaScript Object without jQuery?

梦想的初衷 提交于 2019-12-24 11:53:53
问题 I'm looking for an dependency-free implementation of jQuery.param(). I'm trying to create a serialized representation of an array or object, suitable for use in a URL query string or Ajax request, without using jQuery. I searched for some time now, but everyone just seems to use $.param()... Thank you in advance! 回答1: I found the best and optimal way on How to get query string values using JavaScript. Checkout the below example to fetch the query string. var queryString = window.location

JsonSerializerSettings to change case of property name but not name of property's property

僤鯓⒐⒋嵵緔 提交于 2019-12-24 08:16:37
问题 I am using below settings for camel casing of my class property. JsonSerializerSettings settings = new JsonSerializerSettings() { ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver() }; Some of the property in the object are of some other class type. I do not want to change case of those properties. Eg: Public Class CaseToChange { public string StringProperty{get;set;} //Change to camelCase public SomeOtherType OtherTypeProperty{get;set;} //Change name

error CS0234: The type or namespace name 'Script' does not exist in the namespace 'System.Web'

冷暖自知 提交于 2019-12-22 01:37:10
问题 I am trying to use JavaScriptSerializer in my application. I initially received Cannot find JavaScriptSerializer and I solved it by adding: using System.Web.Script.Serialization; But then the sub-keyword Script is underlined with a blue line: The type or namespace 'Script' does not exist in the namespace 'System.Web' (are you missing an assembly reference?) So I added to the project a reference to: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.0\System.Web

How to serialize Map<String, List<Object>> with FlexJSON

浪尽此生 提交于 2019-12-21 20:52:56
问题 I have a object which I want to serialize to JSON. This object is a map with list of a specific objects. This looks similar to it: Map<String, List<Object>> map = new Map<String, List<Object>>(); I am using FlexJSON. I can work only with flexjson.JSONSerializer . There are my tries: JSONSerializer jsonSerializer = new JSONSerializer(); // jsonSerializer.include("map"); // jsonSerializer.include("map.myList.object"); jsonSerializer.transform(new MapTransformer(), "map"); jsonSerializer.exclude