json.net

Get the name of a JObject in Json.Net

邮差的信 提交于 2019-12-05 06:37:45
I have a JObject equal to: "Info": { "View":"A", "Product":"B", "Offer":"Offer1", "Demo":"body {background-color:red;} #box {border:dotted 50px red;}", "Log":false } How can I return the name of the object, "Info"? I am currently using the Path property like so: jObject.Name = jObject.Path.Substring(jObject.Path.jObject('.') + 1); Is there a better way to do this? In JSON, objects themselves do not have names. An object is just a container for a collection of name-value pairs, beginning and ending with curly braces. So what you have above is a fragment of a larger body of JSON. There must be

How to get first key from JObject?

ぐ巨炮叔叔 提交于 2019-12-05 06:27:24
I am using Newtonsoft.Json in my project. I have JObject like this: { "4781":"Name 1", "1577":"Name 2", "9973":"Name 3" } I successfully parse it with JObject.Parse() . I need to get first key from this JObject ("4781"). How do I get it? Json.NET doesn't directly provide integer indexed access to the properties of a JObject . If you do JObject.Parse(jsonString)[0] you get an ArgumentException with the message Accessed JObject values with invalid key value: 0. Object property name expected." Demo #1 here . I suspect Json.NET was implemented that way because the JSON standard states, "An object

Can't make MVC4 WebApi include null fields in JSON

主宰稳场 提交于 2019-12-05 06:26:18
I'm trying to serialize objects as JSON with MVC4 WebAPI (RTM - just installed VS2012 RTM today but was having this problem yesterday in the RC) and I'd like for all nulls to be rendered in the JSON output. Like this: [{"Id": 1, "PropertyThatMightBeNull": null},{"Id":2, "PropertyThatMightBeNull": null}] But what Im getting is [{"Id":1},{"Id":2}] I've found this Q/A WebApi doesnt serialize null fields but the answer either doesn't work for me or I'm failing to grasp where to put the answer. Here's what I've tried: In Global.asax.cs's Application_Start, I added: var json = GlobalConfiguration

Deserialize JSON not working with JSON.NET

醉酒当歌 提交于 2019-12-05 06:19:45
I have a problem with the following JSON when deserializing using JSON.NET. { "?xml": { "@version": "1.0", "@encoding": "utf-8" }, "Persons": { "Person": [{ "@Id": "1", "@Name": "John", "@Surname": "Smith" }, { "@Id": "2", "@Name": "John", "@Surname": "Smith", "Skills": { "Skill": [{ "@Id": "1", "@Name": "Developer" }, { "@Id": "2", "@Name": "Tester" }] } }] } } I'm using the following classes: public class RootObject { public Xml xml { get; set; } public Persons Persons { get; set; } } public class Xml { public string version { get; set; } public string encoding { get; set; } } public class

JSon.NET DeserializeObject<List<T>> not returning list nor giving error

核能气质少年 提交于 2019-12-05 05:19:14
I got stuck with Json.NET library and its DeserializeObject method. The Documentation is not quite clear with what could be happening here, so I would appreciate if somebody could explain how to acheive to deserialize JSON into list of User objects. I'm trying to deserialize this JSON [ {"userid":"0", "listid":1, "lastname":"Mann", "inplace":true, "xpos":428, "ypos":111 }, {"userid":"1", "listid":1, "lastname":"Parker", "inplace":true, "xpos":334, "ypos":154 }, {"userid":"2", "listid":1, "lastname":"Terry", "inplace":true, "xpos":513, "ypos":160 } ] into an User object [JsonObject

Parse a Json Array in to a class in c#

我怕爱的太早我们不能终老 提交于 2019-12-05 04:46:20
I parsed this single Json : { "text": "Sample Text", "id": 123456789, "user": { "name": "ExampleUser", "id": 123, "screen_name": "ExampleUser" }, "in_reply_to_screen_name": null, } to my c# class RootObject : public class User { public string name { get; set; } public int id { get; set; } public string screen_name { get; set; } } public class RootObject { public string text { get; set; } public long id { get; set; } public User user { get; set; } public object in_reply_to_screen_name { get; set; } } like this : RootObject h = JsonConvert.DeserializeObject<RootObject>(string); All of this

Improve performance of XmlSerializer

风格不统一 提交于 2019-12-05 04:43:58
I use a XmlSerializer to serialize/deserialize some objects. The problem is the performance. When profiling, using the XmlSerializer make our application 2 seconds longer to start. We cache our XmlSerializer and reuse them. We cannot use sgen.exe because we are creating the XmlSerializer with XmlAttributeOverrides . I try to use serialization alternative like Json.Net and, at first, it's work great. The problem is that we need to be backward compatible so all the xml already generated need to be parsed correctly. Also, the object serialization output must be Xml. To summarize: I receive Xml

How to get a string value from a JToken

社会主义新天地 提交于 2019-12-05 04:36:28
I'm getting data from a web service that returns a JSON response. This is my code: WebClient client = new WebClient(); var result = client.DownloadString("http://some url"); JObject obj = JObject.Parse(result); // Location l = new Location(); // l.city = obj["ad"][2]; error here At this point it returns a result, but I am getting an error: Cannot implicitly convert type 'Newtonsoft.Json.Linq.JToken' to 'string' I would like some assistance getting the returned data into my variable in the model. This is my JSON: { data: [ { address_obj: { street1: "9518 Front Beach Road", street2: "", city:

Using JSON.Net to write a property name

霸气de小男生 提交于 2019-12-05 03:59:36
I am using JSON.net to write some json in C#. I can produce JSON like this { "id": "234", "name": "abc" } What i would like to do is get is this { "DATA": { "id": "234", "name": "abc" } } Here is the json.net code i'm using StringBuilder sb = new StringBuilder(); StringWriter sw = new StringWriter(sb); JsonWriter jsonWriter = new JsonTextWriter(sw); jsonWriter.Formatting = Formatting.Indented; jsonWriter.WriteStartObject(); jsonWriter.WritePropertyName("id"); jsonWriter.WriteValue("234"); jsonWriter.WritePropertyName("name"); jsonWriter.WriteValue("abc"); jsonWriter.WriteEndObject(); can you

Cannot deserialize the current JSON object (e.g. {“name”:“value”})

回眸只為那壹抹淺笑 提交于 2019-12-05 03:50:26
问题 This is my JSON data { "logInResult": [ { "Name": "yogesh singh", "cityName": "", "img": "DefaultImage/D_Vp_Men.png", "usrId": "374" } ] } and this is my code public async Task<ActionResult> Index() { HttpClient webClient1 = new HttpClient(); Uri uri = new Uri("http://m.vinipost.com/service/userprofile.svc/logIn?loginId=thyschauhan@gmail.com&pass=12345"); HttpResponseMessage response1; response1 = await webClient1.GetAsync(uri); var jsonString = await response1.Content.ReadAsStringAsync();