[
{
"data": {
"user": {
"id": "12345678",
"displayName": "name",
"subscriptionProducts": [
{
"id": "123456",
"emoteSetID": "123456",
"name": "name",
"__typename": "SubscriptionProduct"
},
{
"id": "123456",
"emoteSetID": "123456",
"name": "name2000",
"__typename": "SubscriptionProduct"
},
{
"id": "123456",
"emoteSetID": "123456",
"name": "name_3000",
"__typename": "SubscriptionProduct"
}
],
"self": null,
"__typename": "User"
},
"currentUser": null,
"requestInfo": {
"countryCode": "US",
"__typename": "RequestInfo"
}
},
"extensions": {
"durationMilliseconds": 31,
"operationName": "ChannelPage_SubscribeButton_User",
"requestID": "1234124124asd123412425151245124"
}
}
]
I get this response in my C# application and I am trying to just grab the "ID" which is 12345678
after {"data":{"user":{"id":"12345678"... blah blah blah
I wrote a code but it does not work
string value = httpreqResp.Split(new[] { "/"id/": /"" }, StringSplitOptions.None)[1].Split('"')[0];
Can someone help if possible
You go to http://QuickType.io and paste your json in
You set some other options like the name of the root class
It generates you a bunch of C# - I would have done this with your json but I'm on a cellphone and QTIO doesn't work on a cell; can't paste the json in, so I had to use the example temperatures.json
The generated C# looks like this, a host of classes that represent the objects in the JSON then some handy one shot methods for converting from/to the JSON:
// <auto-generated />
//
// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do:
//
// using QuickType;
//
// var temperatures = Temperatures.FromJson(jsonString);
namespace QuickType
{
using System;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
public partial class Temperatures
{
[JsonProperty("description")]
public Description Description { get; set; }
[JsonProperty("data")]
public Dictionary<string, Datum> Data { get; set; }
}
public partial class Datum
{
[JsonProperty("value")]
public string Value { get; set; }
[JsonProperty("anomaly")]
public string Anomaly { get; set; }
}
public partial class Description
{
[JsonProperty("title")]
public string Title { get; set; }
[JsonProperty("units")]
public string Units { get; set; }
[JsonProperty("base_period")]
public string BasePeriod { get; set; }
}
public partial class Temperatures
{
public static Temperatures FromJson(string json) => JsonConvert.DeserializeObject<Temperatures>(json, QuickType.Converter.Settings);
}
public static class Serialize
{
public static string ToJson(this Temperatures self) => JsonConvert.SerializeObject(self, QuickType.Converter.Settings);
}
internal static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters =
{
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
},
};
}
}
you get your response string str from the server
you write a line of code like var parsed = YourRootClassName.FromJson(str)
and then your desired data is available in e.g parsed[0].Data.User.Id
You can do a similar thing with a visual studio's "Paste Json as classes" function, except it doesn't write the handy deserializer bits for you like QuickType does. Those parts don't change though, so if QTIO ever went away, this answer could still stand by blending the deser methods above with the resulting c# from doing a paste as classes
If you really want to do it using string split, just split on " and it'll be the seventh element of the resulting array. Maybe. It'll be fragile as hell, which is why you should parse it
Edit: json2charp can be used from a cell, their version looks like this:
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class SubscriptionProduct {
public string id { get; set; }
public string emoteSetID { get; set; }
public string name { get; set; }
public string __typename { get; set; }
}
public class User {
public string id { get; set; }
public string displayName { get; set; }
public List<SubscriptionProduct> subscriptionProducts { get; set; }
public object self { get; set; }
public string __typename { get; set; }
}
public class RequestInfo {
public string countryCode { get; set; }
public string __typename { get; set; }
}
public class Data {
public User user { get; set; }
public object currentUser { get; set; }
public RequestInfo requestInfo { get; set; }
}
public class Extensions {
public int durationMilliseconds { get; set; }
public string operationName { get; set; }
public string requestID { get; set; }
}
public class MyArray {
public Data data { get; set; }
public Extensions extensions { get; set; }
}
public class Root {
public List<MyArray> MyArray { get; set; }
}
And would be used like:
Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(responseStringFromWebservice);
var id = myDeserializedClass.MyArray[0].Data.User.id;
This is a JSON string,
You can JsonSerializer.Deserialize to convert it to a C# class.
First create your own classes to store data,like this
public class SubscriptionProduct {
public string id { get; set; }
public string emoteSetID { get; set; }
public string name { get; set; }
public string __typename { get; set; }
}
public class User {
public string id { get; set; }
public string displayName { get; set; }
public List<SubscriptionProduct> subscriptionProducts { get; set; }
public object self { get; set; }
public string __typename { get; set; }
}
public class RequestInfo {
public string countryCode { get; set; }
public string __typename { get; set; }
}
public class Data {
public User user { get; set; }
public object currentUser { get; set; }
public RequestInfo requestInfo { get; set; }
}
public class Extensions {
public int durationMilliseconds { get; set; }
public string operationName { get; set; }
public string requestID { get; set; }
}
public class MyArray {
public Data data { get; set; }
public Extensions extensions { get; set; }
}
public class MyClass {
public List<MyArray> MyArray { get; set; }
}
Then use this:
var mydata = JsonSerializer.Deserialize<MyClass>(jsonString);
Related
I have the below response from the API in a console application written in C#:
{
"d": {
"results": [
{
"__metadata": {
"id": "123",
"uri": "456",
"type": "789"
},
"PERNR": "1",
"USRID": "2"
},
{
"__metadata": {
"id": "159",
"uri": "951",
"type": "753"
},
"PERNR": "4",
"USRID": "6"
}
]
}
}
And used the below code to deserialize:
public class d
{
public results results { get; set; }
}
public class results
{
public string PERNR { get; set; }
public string USRID { get; set; }
public __metadata __metadata { get; set; }
}
public class __metadata
{
public string id { get; set;}
public string uri { get; set; }
public string type { get; set; }
}
var serilizer = new JavaScriptSerializer();
d output = serilizer.Deserialize<d>(response.Content);
But the result is null. Is there any problem in the definition of the classes?
The properties should start with a capital letter, to match the case of the properties in the JSON response. Change the class definitions as follows:
public class D
{
public Results[] results { get; set; }
}
public class Results
{
public string PERNR { get; set; }
public string USRID { get; set; }
public Metadata __metadata { get; set; }
}
public class Metadata
{
public string id { get; set;}
public string uri { get; set; }
public string type { get; set; }
}
Change deserialize line to:
var serilizer = new JavaScriptSerializer();
D output = serilizer.Deserialize<D>(response.Content);
The issue is results is an array in your json, where in your class, it is an object. Change it to
public class d
{
public Result[] results { get; set; }
}
If you use a JSON to C# converter like json2csharp or Visual Studio's Paste JSON As Classes you'll get :
public class Root
{
public D d { get; set; }
}
public class D
{
public List<Result> results { get; set; }
}
public class Metadata
{
public string id { get; set; }
public string uri { get; set; }
public string type { get; set; }
}
public class Result
{
public Metadata __metadata { get; set; }
public string PERNR { get; set; }
public string USRID { get; set; }
}
There are two important differences :
The JSON document contains a root element with an attribute named d, not a D object
D.results is a collection of Result objects, not a single object
I noticed that I would get different results when deserializing objects different ways. I must have missed something but couldn't figure out what.
Here is the json:
{
"content_version": "1",
"date_created": "2020-10-06T13:52:15.288Z",
"date_updated": "2020-10-06T13:54:24.325Z",
"content": {
"EN": {
"name_full": {
"text": "Test"
},
"name_short": {
"text": "TEST"
}
}
},
"tools": {
"car": true,
"truck": true,
"fleet": {
"use": true,
"languages": [
{
"fileName": "file1.json",
"name": {
"text": "English"
}
}
]
}
}
}
The following are the classes that I generated using a tool:
public class Root
{
public string content_version { get; set; }
public DateTime date_created { get; set; }
public DateTime date_updated { get; set; }
public ContentRootObject content { get; set; }
public Tools tools { get; set; }
}
public class ContentRootObject
{
[JsonProperty(PropertyName = "content")]
public Dictionary<string, ContentItem> DataItems { get; set; }
}
public class ContentItem
{
public NameFull name_full { get; set; }
public NameShort name_short { get; set; }
}
public class Tools
{
public bool car { get; set; }
public bool truck { get; set; }
public Fleet fleet { get; set; }
}
public class NameFull
{
public string text { get; set; }
}
public class NameShort
{
public string text { get; set; }
}
public class EN
{
public NameFull name_full { get; set; }
public NameShort name_short { get; set; }
}
public class Name
{
public string text { get; set; }
}
public class Language
{
public string fileName { get; set; }
public Name name { get; set; }
}
public class Fleet
{
public bool use { get; set; }
public List<Language> languages { get; set; }
}
and here is the code that I tried for deserializing :
var objTool = Newtonsoft.Json.JsonConvert.DeserializeObject<Tools>(json);
var objRoot = Newtonsoft.Json.JsonConvert.DeserializeObject<Root>(json);
var objContent = Newtonsoft.Json.JsonConvert.DeserializeObject<ContentRootObject>(json)
and here is what I got for each of the objects:
objTool: 'fleet' was returned as null in 'tools'
objRoot: the DataItems of 'content' was null.
objContent: got an error saying "Unexpected character encountered while parsing value: {. Path 'content.EN', line 1, position 146."
Although I can just deserialize the whole thing, I only need objTool and objContent and I was wondering what I did wrong in the above code? Thanks!
The issue is that the class that you have as a wrapper for the dictionary is not represented in the JSON. It'll deserialize the dictionary if you change the root object to this:
public class Root
{
public string content_version { get; set; }
public DateTime date_created { get; set; }
public DateTime date_updated { get; set; }
public Dictionary<string, ContentItem> content { get; set; }
public Tools tools { get; set; }
}
I have referred to this question, which is similar to my issue but unable to fix the issue completely since the data structure is different and I am not able to figure-out how to apply this solution to my example data given below:
{
"result": {
"RITM2572913": {
"number": "RITM2572913",
"state": "1",
"stage": "fulfillment",
"Sys_ID": "220e89681b31b384e3a0a79b2d4bcbf3",
"requested_for": "1d1673c4dbda5b0072a85099dc9619b0",
"Contoso_requested_for": "requested_for:1d1673c4dbda5b0072a85099dc9619b0,var_name_arr:",
"Contoso_sc_Purposeofthef5request": "Add",
"Contoso_Sc_Contactinfo": "Contact ",
"Contoso_sc_Appname": "Application ",
"Contoso_sc_Description": "Description",
"Contoso_special_instructions": "special_instructions:",
"business_justification": "Justification ",
"Contoso_business_justification": "busess_justification:Justification",
"Contoso_catalog_item_footer": "owner_info:"
}
}
}
I have the response data like this and need to de-serialize it to fit in the object model given below:
public class RITMGETRequestResponse
{
public RITMDetails result { get; set; }
public class RITMDetails
{
public string business_justification { get; set; }
public string number { get; set; }
public string requested_for { get; set; }
public string stage { get; set; }
public string state { get; set; }
public string Sys_ID { get; set; }
public string var_name_arr { get; set; }
public string Contoso_business_justification { get; set; }
public string Contoso_catalog_item_footer { get; set; }
public string Contoso_requested_for { get; set; }
public string Contoso_sc_Appname { get; set; }
public string Contoso_Sc_Contactinfo { get; set; }
public string Contoso_sc_Description { get; set; }
public string Contoso_sc_Purposeofthef5request { get; set; }
public string Contoso_special_instructions { get; set; }
}
}
In this case RITM number is dynamic. I need to get the Sys_ID and other properties of this JSON. How do I de-serialize this JSON response to get these values?
straightforward example:
used a JSONProperty attribute to map result values of a dynamic property name
class Program
{
static void Main(string[] args)
{
var deserialise = JsonConvert.DeserializeObject<RITMRequestResponse>("{\"result\": {\"123\" : { \"number\" : \"123\" }}}");
Console.WriteLine(deserialise);
Console.ReadLine();
}
}
public class RITMRequestResponse
{
[JsonProperty(PropertyName = "result")]
public Dictionary<string, RITMDetails> RITMDetails { get; set; }
}
public class RITMDetails
{
public string Number { get; set; }
}
I have a collection of classes set out like this:
public class Xml
{
public string version { get; set; }
public string encoding { get; set; }
}
public class Content
{
public string Expires { get; set; }
public string MaxArrivalScope { get; set; }
}
public class Trip
{
public string ETA { get; set; }
public string TripNo { get; set; }
public string WheelchairAccess { get; set; }
}
public class Destination
{
public string Name { get; set; }
public List<Trip> Trip { get; set; }
}
public class Route
{
public string RouteNo { get; set; }
public string Name { get; set; }
public Destination Destination { get; set; }
}
public class Platform
{
public string PlatformTag { get; set; }
public string Name { get; set; }
public Route Route { get; set; }
}
public class JPRoutePositionET
{
public string xmlns { get; set; }
public string xsi { get; set; }
public string schemaLocation { get; set; }
public Content Content { get; set; }
public Platform Platform { get; set; }
}
public class RootObject
{
public Xml xml { get; set; }
public JPRoutePositionET JPRoutePositionET { get; set; }
}
}
I have JSON like this:
{
"xml": {
"version": "1.0",
"encoding": "utf-8"
},
"JPRoutePositionET": {
"xmlns": "urn:connexionz-co-nz:jp",
"xsi": "http://www.w3.org/2001/XMLSchema-instance",
"schemaLocation": "urn:connexionz-co-nz:jp JourneyPlanner.xsd",
"Content": {
"Expires": "2017-04-09T15:59:31+12:00",
"MaxArrivalScope": "60"
},
"Platform": {
"PlatformTag": "2628",
"Name": "Awatea Rd near Awatea Gdns",
"Route": {
"RouteNo": "125",
"Name": "Redwood/Westlake",
"Destination": {
"Name": "Westlake & Halswell",
"Trip": [
{
"ETA": "4",
"TripNo": "4751",
"WheelchairAccess": "true"
},
{
"ETA": "32",
"TripNo": "4752",
"WheelchairAccess": "true"
}
]
}
}
}
}
}
Why is Newtonsoft not parsing correctly to the Platform class? It returns null. Will I need to format the JSON to cut out all the other information I don't want? Or is it something I am missing?
Fabio's Comment of
var result = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(jsonString);
Worked.
But now when I get JSON like pastebin.com/pebp178s I can't seem to use Json2CSharp to get the classes I'd need as it doesn't make Destination a List, it also changes Trip to an Object instead of a class. Which is really weird. It just throws an error saying the JSON can't be parsed.
I have json string. I want to retrieve the contact from json string. Following json contains array of contacts. here is my json string.
{
"contacts": {
"contact": [
{
"isConnection": false,
"id": 33554611,
"fields": [
{
"id": 33554748,
"type": "name",
"value": {
"givenName": "Jhon",
"middleName": "",
"familyName": "Scot",
"prefix": "",
"suffix": "",
"givenNameSound": "",
"familyNameSound": ""
},
"editedBy": "OWNER",
"flags": [],
"categories": [],
"updated": "2012-12-23T07:40:23Z",
"created": "2012-12-23T07:40:23Z",
},
{
"id": 33554749,
"type": "email",
"value": "someone#example.com",
"editedBy": "OWNER",
"flags": [],
"categories": [],
"updated": "2012-12-23T07:40:23Z",
"created": "2012-12-23T07:40:23Z",
}
]
}
}
}
Here I want to retrieves the values of givenName,familyName,email. How can I retrieve the values of these from json string.
Note: there are array of contact in json. I have posted only one contact from this json.
I tried something like this. But not worked.
JObject json = JObject.Parse(returnStr);
JArray fields = (JArray)json["contacts"]["contact"]["fields"][0];
JArray FValues = (JArray)json["contact"]["fields"]["value"];
I tried this
public class Field
{
public int id { get; set; }
public string type { get; set; }
public object value { get; set; }
public string editedBy { get; set; }
public List<object> flags { get; set; }
public List<object> categories { get; set; }
public string updated { get; set; }
public string created { get; set; }
public string uri { get; set; }
public bool? isConnection { get; set; }
}
public class contact
{
public bool isConnection { get; set; }
public int id { get; set; }
public List<Field> fields { get; set; }
public List<object> categories { get; set; }
public int error { get; set; }
public int restoredId { get; set; }
public string created { get; set; }
public string updated { get; set; }
public string uri { get; set; }
}
public class Contacts
{
public List<contact> contact { get; set; }
public int count { get; set; }
public int start { get; set; }
public int total { get; set; }
public string uri { get; set; }
public bool cache { get; set; }
}
public class RootObject
{
public Contacts contacts { get; set; }
}
and
JavaScriptSerializer serializer1 = new JavaScriptSerializer();
RootObject obje = serializer1.Deserialize<RootObject>(returnStr);
But it is giving me 0 value in obje.
First make sure your Json is in valid format using jsonlint
Then generate class base on it using json2csharp
public class Field
{
public int id { get; set; }
public string type { get; set; }
public object value { get; set; }
public string editedBy { get; set; }
public List<object> flags { get; set; }
public List<object> categories { get; set; }
public string updated { get; set; }
public string created { get; set; }
}
public class Contact
{
public bool isConnection { get; set; }
public int id { get; set; }
public List<Field> fields { get; set; }
}
public class Contacts
{
public List<Contact> contact { get; set; }
}
public class RootObject
{
public Contacts contacts { get; set; }
}
Use Newtonsoft JSON to deserialize your Json into object(s) then you may simply access its properties value.
JsonConvert.DeserializeObject<RootObject>(string json);
Class for json object (generated with http://jsonutils.com/ after correcting some syntax error):
public class Field
{
public int id { get; set; }
public string type { get; set; }
public object value { get; set; }
public string editedBy { get; set; }
public IList<object> flags { get; set; }
public IList<object> categories { get; set; }
public DateTime updated { get; set; }
public DateTime created { get; set; }
}
public class Contact
{
public bool isConnection { get; set; }
public int id { get; set; }
public IList<Field> fields { get; set; }
}
public class Contacts
{
public IList<Contact> contact { get; set; }
}
public class Example
{
public Contacts contacts { get; set; }
}
Deserialization (you will probably need to add a reference to System.Web.Extensions):
System.Web.Script.Serialization.JavaScriptSerializer deSer = new System.Web.Script.Serialization.JavaScriptSerializer();
JSonPrintSettingsToXml.Input.Example deserializedJSON = deSer.Deserialize<JSonPrintSettingsToXml.Input.Example>(yourJSON);
Here is the corrected JSON
{
"contacts": {
"contact": [
{
"isConnection": false,
"id": 33554611,
"fields": [
{
"id": 33554748,
"type": "name",
"value": {
"givenName": "Jhon",
"middleName": "",
"familyName": "Scot",
"prefix": "",
"suffix": "",
"givenNameSound": "",
"familyNameSound": ""
},
"editedBy": "OWNER",
"flags": [],
"categories": [],
"updated": "2012-12-23T07:40:23Z",
"created": "2012-12-23T07:40:23Z"
},
{
"id": 33554749,
"type": "email",
"value": "someone#example.com",
"editedBy": "OWNER",
"flags": [],
"categories": [],
"updated": "2012-12-23T07:40:23Z",
"created": "2012-12-23T07:40:23Z"
}
]
}
]
}
}
You need to go with the following structure:
public class Contact
{
public bool isConnection { get; set; }
public int id { get; set; }
public List<Field> fields { get; set; }
}
public class Field
{
public int id { get; set; }
public string type { get; set; }
public object value { get; set; }
public string editedBy { get; set; }
public string[] flags { get; set; }
public string[] categories { get; set; }
public DateTime updated { get; set; }
public DateTime created { get; set; }
}
public class Name
{
public string givenName { get; set; }
public string middleName { get; set; }
public string familyName { get; set; }
public string prefix { get; set; }
public string suffix { get; set; }
public string givenNameSound { get; set; }
public string familyNameSound { get; set; }
}
And then deserialize it and use LINQ to manipulate fields.
If you want to stick to JObject and not create classes, look at the example provided at http://weblog.west-wind.com/posts/2012/Aug/30/Using-JSONNET-for-dynamic-JSON-parsing
You'll have to enumerate over this
dynamic contacts = (JArray)json["contacts"]
foreach(dynamic contact in contacts.contact) {
// look at the fields...
}
PS. Give it a go, don't have VS on me so cant quite verify the exact syntax
Firstly, I think you'll find that your JSON is not well-formed. You don't need the extra commas after the two "created" dates and there is a right-square bracket missing before the second last curly brace. I recommend you always validate your JSON using this awesome site: http://jsonformatter.curiousconcept.com
Secondly, you are not referencing the array elements correctly. Although I agree with #grundy that you should be creating class definitions for managing JSON and using LINQ, there is absolutely nothing wrong with the Newtonsoft library. You can still persist with your method.
Try this:-
var json = JObject.Parse(returnStr);
var fields = (JArray)json["contacts"]["contact"][0]["fields"];
var givenName = fields[0]["value"]["givenName"];
var familyName = fields[0]["value"]["familyName"];
var email = fields[1]["value"];