Deserializing JSON object and Query - c#

I need to query for and get the matched JSON string. Following is my JSON:
I need to query the JSON I receive in the HTTP RESPONSE, match the JSON where code=2, then extract the text=Jenny kisworth
JSON
[
{
"code":1234,
"parentCode":9898,
"language":{
"lookup": "IN",
"code": 1
},
"parentType": "Patient",
"text": "James Berth"
},
{
"code":4567,
"parentCode":8989,
"language":{
"lookup": "IN",
"code": 1
},
"parentType": "Patient",
"text": "James Bond"
},
{
"code":89101,
"parentCode":2525,
"language":{
"lookup": "OUT",
"code": 2
},
"parentType": "Patient",
"text": "Jenny kisworth"
}
]
CODE:
public class JSonData
{
[Newtonsoft.Json.JsonProperty("code")]
public string code { get; set; }
[Newtonsoft.Json.JsonProperty("language")]
public List<Datum> language { get; set; }
}
public class Datum
{
public string lookup { get; set; }
public int code { get; set; }
}
//only posting code relevant to the subject
HttpResponseMessage responseCode = client.GetAsync(codeParameters).Result;
if (responseCode.IsSuccessStatusCode)
{
var dataObjects = responseAlternateTitles.Content.ReadAsStringAsync();
dataObjects.Wait();
string dataObjectsString = dataObjects.Result.ToString();
var data = Newtonsoft.Json.JsonConvert.DeserializeObject<List<JSonData>>(dataObjectsString);
}
In the above I get an error: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List1[BCMTest.Datum]' because the type requires a JSON array`

Your classes should look more like this, how do i know? http://json2csharp.com/
public class Language
{
public string lookup { get; set; }
public int code { get; set; }
}
public class JSonData
{
public int code { get; set; }
public int parentCode { get; set; }
public Language language { get; set; }
public string parentType { get; set; }
public string text { get; set; }
}
...
var data = Newtonsoft.Json.JsonConvert.DeserializeObject<List<JSonData>>(dataObjectsString);

You are getting an error because your JSON doesnot have a object of array in language as you are expecting in your class object.
Change the JSonData class -> language
public class JSonData
{
[Newtonsoft.Json.JsonProperty("code")]
public string code { get; set; }
[Newtonsoft.Json.JsonProperty("language")]
public Datum language { get; set; }
}

public class Language
{
public string lookup { get; set; }
public int code { get; set; }
}
public class JSonData
{
[Newtonsoft.Json.JsonProperty("code")]
public string code { get; set; }
[Newtonsoft.Json.JsonProperty("parentCode")]
public int parentCode { get; set; }
[Newtonsoft.Json.JsonProperty("language")]
public Language language { get; set; }
[Newtonsoft.Json.JsonProperty("parentType")]
public string parentType { get; set; }
[Newtonsoft.Json.JsonProperty("text")]
public string text { get; set; }
}
var data = Newtonsoft.Json.JsonConvert.DeserializeObject<List<JSonData>>(dataObjectsString);
var filtereddata = data.Where(s => s.language.code.Equals(2));

Related

Problem Deserializing JSON to objects in C#

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

C#, a String's Split() method getting certain information from string

[
{
"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);

Unable to Parsing nested Json file in C#

I have Json format response from a API call and I want to map the data from the response to each varibales.
Json format
{
"success": true,
"data": {
"students": [
{
"Admission_date": "2018-05-01",
"Name": "Sree",
"Branch": "Electronics",
"Semester": "2",
"HOD": "Mahesh",
},
{
"Admission_date": "2018-05-01",
"Name": "Naresh",
"Branch": "Electronics",
"Semester": "2",
"HOD": "Mahesh",
}
],
"remaining": 0
}
}
I have tried to parse the JSON response and then to load the value through for each. But I'm not able to achieve the solution.
JObject jsonparsing1 = JObject.Parse(str4); //str4;- Json value
var token1 = (JArray)jsonparsing1.SelectToken("data");
var token2 = (JArray)jsonparsing1.SelectToken("data[0]Students");
JArray abc = JsonConvert.DeserializeObject<JArray>(token2.ToString());
foreach (var test in abc)
{
String Admission_date=test["Admission_date"];
String Name=test["Name"];
String Branch=test["Branch"];
String Semester=test["Semester"];
String HOD=test["HOD"];
String remaining=test["remaining"];
}
Expected result
String Admission_date=Admission_date
String Name=Name
String Branch=Branch
String Semester=Semester
String HOD=HOD
String remaining=remaining
Could anyone please help me on this?
I think you can use this sample:
public class JsonData
{
public bool success { get; set; }
public Data data { get; set; }
}
public class Data
{
public Data()
{
this.students = new List<Student>();
}
public List<Student> students { get; set; }
public int remaining { get; set; }
}
public class Student
{
public string Admission_date { get; set; }
public string Name { get; set; }
public string Branch { get; set; }
public string Semester { get; set; }
public string HOD { get; set; }
}
And then:
JsonData abc = JsonConvert.DeserializeObject<JsonData>(token2.ToString());
I will do this way!
public class Student
{
public string Admission_date { get; set; }
public string Name { get; set; }
public string Branch { get; set; }
public string Semester { get; set; }
public string HOD { get; set; }
}
public class Data
{
public List<Student> students { get; set; }
public int remaining { get; set; }
}
public class RootObject
{
public bool success { get; set; }
public Data data { get; set; }
}
and in C# Code Just use only below line:
var obj = JsonConvert.DeserializeObject<RootObject>("{ \"success\": true,\"data\": {\"students\": [{ \"Admission_date\": \"2018-05-01\",\"Name\": \"Sree\",\"Branch\":\"Electronics\",\"Semester\": \"2\",\"HOD\": \"Mahesh\",}],\"remaining\": 0}}");
use
foreach(var item in obj.data.students)
{
// Access Admission_date etc.
string name = item.Name;
}
dotnetfiddle

JSON not deserializing as expected?

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>(js‌​onString);
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.

Deserializing a complex JSON object

I use JavaScript.Serializer.Deserializer to deserialize a complex JSON object, as below:
{
"name": "rule 1",
"enabled": true,
"conditions": [{
"type": "time",
"time": "17:23:10",
"days": "125"
}, {
"type": "sensor",
"uid": "10.2.0.1",
"setpoint1": 12,
"setpoint2": 18,
"criterion": 1
}, {
"type": "sensor",
"uid": "10.3.0.1",
"setpoint1": 12,
"setpoint2": 18,
"criterion": 2
}],
"actions": {
"period": 100,
"single": false,
"act": [{
"type": "on",
"uid": "10.1.0.1"
}, {
"type": "sms",
"message": "Hello World"
}]
}
}
And I want to convert it to some classes, like below:
public class Rule
{
public string name { get; set; }
public bool enabled { get; set; }
public List<Condition> conditions { get; set; }
public List<Action> actions { get; set; }
}
public class Condition
{
public string type { get; set; }
public string uid { get; set; }
public DateTime? time { get; set; }
public string days { get; set; }
public double setpoint1 { get; set; }
public double setpoint2 { get; set; }
public int criterion { get; set; }
}
public class Action
{
public int period { get; set; }
public bool single { get; set; }
public List<Act> act { get; set; }
}
public class Act
{
public string type { get; set; }
public string uid { get; set; }
public string message { get; set; }
}
The deserialization snippet:
json = new JavaScriptSerializer();
Rule rule = (json.Deserialize<Rule>(jsonStr));
If I simplify the Rule class and declare conditions and actions as simple strings, it works fine.
But if I use the classes like above, it throws an exception:
Cannot convert object of type 'System.String' to type 'System.Collections.Generic.List`1[IoTWebApplication.Condition]'
The structure you create does not fit to the JSON you posted.
It should look like
public class Rule
{
public string name { get; set; }
public bool enabled { get; set; }
public Condition[ ] conditions { get; set; }
public Actions actions { get; set; }
}
public class Actions
{
public int period { get; set; }
public bool single { get; set; }
public Act[ ] act { get; set; }
}
public class Act
{
public string type { get; set; }
public string uid { get; set; }
public string message { get; set; }
}
public class Condition
{
public string type { get; set; }
public string time { get; set; }
public string days { get; set; }
public string uid { get; set; }
public int setpoint1 { get; set; }
public int setpoint2 { get; set; }
public int criterion { get; set; }
}
It is (in most cases) very easy in VS to get the classes direct out of the JSON
Copy JSON to clipboard
In VS EDIT/Special Paste/Paste JSON as Classes (the code above was created by this)
The problem was that the inner (nested) json was quoted and therefore was processed as a string. So when I removed the quotes, it worked fine:
json = new JavaScriptSerializer();
Rule rule = (json.Deserialize<Rule>(jsonStr));

Categories

Resources