json.net

Deserializing an empty array using Json.NET

拟墨画扇 提交于 2019-12-12 14:09:34
问题 I've got a C# application that uses Json.NET v7.0.1. As a result of a REST call, I get back some JSON in the form of: { "messages": [ {"phoneNumber":"123-456-7890", "smsText":"abcd1234="}, {"phoneNumber":"234-567-8901", "smsText":"efgh5678="}, {"phoneNumber":"345-678-9012", "smsText":"12345asdf"} ] } If there is no message data to return, the JSON that I get back looks like this: { "messages": [ ] } My Message class looks like this: public class Message { public string PhoneNumber { get; set;

JSON.NET Deserealization

和自甴很熟 提交于 2019-12-12 13:34:23
问题 I have next JSON callback: { "id":"1", "jsonrpc":"2.0", "result":{ "articles":[ { "date":1367582340000, "id":6917, "title":"Some Title", "author":"Name Surname", "event_date":1367584560000, "text":"blabla" } ] } } I want to deserialize it using JSON.NET I have written the next code: class News { private string jsonrpc; private string id; private Result result; [JsonProperty("jsonrpc")] public string Jsonrpc { get { return jsonrpc; } set { jsonrpc = value; } } [JsonProperty("id")] public

Force conversion of empty JSON array to Dictionary type

若如初见. 提交于 2019-12-12 12:36:02
问题 The problem is, that json_encode() function in PHP leaves ambiguity for tools which are reading its output. In PHP both lists and dictionaries are same type of array . echo json_encode([]); // [] echo json_encode(["5" => "something"]); // {"5": "something"} In JSON.NET I want to force both [] and {"5": "something"} to convert to Dictionary<string, string> type. However it recognizes [] as prohibited structure for Dictionary and throws an Exception. Can I quickly leave empty JSON arrays nulled

What's the correct JsonPath expression to search a JSON root object using Newtonsoft.Json.NET?

核能气质少年 提交于 2019-12-12 12:29:36
问题 Most examples deal with the book store example from Stefan Gössner, however I'm struggling to define the correct JsonPath expression for a simple object (no array): { "Id": 1, "Name": "Test" } To check if this json contains Id = 1 . I tried the following expression: $..?[(@.Id == 1]) , but this does find any matches using Json.NET? Also tried Manatee.Json for parsing, and there it seems the jsonpath expression could be like $[?($.Id == 1)] ? 回答1: The path that you posted is not valid. I think

JSON.Net - How to deserialize JSON to object but treating a property as a string instead of JSON?

人走茶凉 提交于 2019-12-12 12:20:58
问题 I have some JSON I would like to deserialize, but I want to treat one of the properties as a string, not an object. As an example the JSON looks like this: { "name":"Frank", "sex":"male", "address": { "street":"nowhere st", "foo":"bar" } } And I want to deserialize it to this object - Treating the address object as a string literal: public class Person { public string name; public string sex; public string address; } I've tried literally deserializing it to this object but get the error:

Identifying that a property's value is an array

别来无恙 提交于 2019-12-12 12:20:53
问题 I have a JSON file: { "abn":"63119059513", "acn":"119059513", "business_structure":"Private Company", "ngr_number":"1231231", "cbh_number":"1231231", "main_name":"Brickworks Building Products Pty Ltd", "trading_name":"Brickworks", "other_trading_names":"Austral Bricks", "directors":[ { "ID":"12114", "ae_forms_filled_in_ID":"22739", "name":"John Smith", "dob":"1983-10-29", "address_line_1":"123 Fake Street", "address_line_2":"", "address_line_city":"Fakeland", "address_line_postcode":"2000",

JSON.NET: Obtain JObject from JProperty Value

笑着哭i 提交于 2019-12-12 12:14:27
问题 I'm stuck on something: I deserialized a JSON file using JObject.Load: // get the JSON into an object JObject jsonObject = JObject.Load(new JsonTextReader(new StreamReader("mydoc.json"))); Fine. I now have a populate jsonObject. Now I iterate through its properties like this: foreach (JProperty jsonRootProperty in jsonObject.Properties()) { if (jsonRootProperty.Name=="Hotel") { ... !!! I just want a JObject here... } } Once I find a property with a Name equal to "Hotel", I want that property

How to change property names depending on the type when serializing with Json.net?

别等时光非礼了梦想. 提交于 2019-12-12 12:13:22
问题 I have a property of type object and I have to change the name depending on which type it has. Should be pretty similar to [XmlElement("PropertyName", typeof(PropertyType))] attribute for XML. For example, I have a property public object Item { get; set; } If at runtime my property has a type of Vehicle , I want to change the name of my property to "Vehicle"; if it has a type of Profile , I want to change the name of my property to "Profile". 回答1: There is no built-in way to dynamically

escape accented chars on utf-8 json

北城以北 提交于 2019-12-12 11:53:11
问题 the code below produce this output: {"x": "Art. 120 - Incapacità di intendere o di volere"} i need to change to this, i suppose i've to change something on encoding but i don't know what: {"x": "Art. 120 - Incapacit\u00e0 di intendere o di volere"} code: string label = "Art. 120 - Incapacità di intendere o di volere"; JObject j = new JObject(); j.Add(new JProperty("x", label)); string s = j.ToString(); Encoding encoding = new UTF8Encoding(false); string filename = @"c:\temp\test.json"; using

SerializeObject throws System.OutOfMemoryException

ぃ、小莉子 提交于 2019-12-12 11:26:16
问题 I have a serious problem with "JsonConvert.SerializeObject" I need to serialize more than 500,000 dictionary records to make serialize throws the following error; System.OutOfMemoryException. I tried to serialize each key, value pair individually in a foreach but it's locked. Apparently it is an optimization problem, but I do not know where to start, threads to serialize in parts? These functions works fine with few values. My code: string json = JsonConvert.SerializeObject