I would like to deserialize a json to object. The json is like below. But one property value maybe string or array, does anyone know how to handle this?
{
"name": "123", //Name
"properties": [
{
"propertyId": "Subject", // property id
"value": [
{
"entityId": "math", //entity id
"entityTypeId": "MATH" //entity type id
}
]
},
{
"propertyId": "Description",
"value": "Hello World."
}
]
}
The class is like below.
//The object
public class Content
{
public Content()
{
//Properties is List.
Properties = new List<Property>();
}
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }
[JsonProperty(PropertyName = "properties")]
public List<Property> Properties { get; set; }
}
public class Property
{
public Property()
{
Value = new List<Value>();
}
[JsonProperty(PropertyName = "propertyId")]
public string PropertyDefId { get; set; }
//Actually this property value can also be string, that's the problem.
[JsonProperty(PropertyName = "value")]
public List<Value> Value { get; set; }
}
//Value object
public class Value
{
//Have to write comments.
[JsonProperty(PropertyName = "entityId")]
public string EntityId { get; set; }
//Have to write comments.
[JsonProperty(PropertyName = "entityTypeId")]
public string EntityTypeId { get; set; }
}
I've done this in Java with Gson liblary and it was like
JsonObject field = parser.parse(json).getElementAsJSONObject();
if (field.isPrimitive()) {
String text = field.asString();
} else if (field.isArray()) {
JSONArray array = field.asArray();
}
I wrote this code from my memory so not 100% reliable. I don't know any solution for C# though.
$.parseJSON will convert your string to the correct object even if the property type is different for two different properties.
http://jsfiddle.net/mdanielc/e0acsyp1/2/
var jsonString = '{"name": "123","properties": [{"propertyId": "Subject","value": [{"entityId":"math","entityTypeId": "MATH" }]},{"propertyId": "Description","value": "Hello World."}]}';
var jsonobj = $.parseJSON(jsonString);
alert(jsonobj.properties[0].value[0].entityId);
alert(jsonobj.properties[1].value);
});
Related
I am trying to use an RESTFUL API for an application we use internally. One call to the API returns the following JSON:
{
"operation": {
"name": "GET RESOURCES",
"result": {
"status": "Success",
"message": "Query was successful"
},
"totalRows": 2,
"Details": [{
"RESOURCE DESCRIPTION": "Windows",
"RESOURCE TYPE": "Windows",
"RESOURCE ID": "101",
"RESOURCE NAME": "WINDOWSPC",
"NOOFACCOUNTS": "1"
}, {
"RESOURCE DESCRIPTION": "Ubuntu",
"RESOURCE TYPE": "Linux",
"RESOURCE ID": "808",
"RESOURCE NAME": "UBUNTUPC",
"NOOFACCOUNTS": "2"
}]
}
}
Using json.net I deseralize the json and check the stats with the following lines:
dynamic json = JsonConvert.DeserializeObject(response);
var status = json.operation.result.status.Value;
Next I want to get each value of each of the "Details" returned, but I cannot figure out how. I first tried getting the Details only with this:
var resourceList = json.operation.Details
Which works, but I cannot iterate over this to get just the "RESOURCE ID" and "RESOURCE NAME" for example.
I cannot use .Children() either, but when I hover over the resourceList there is a ChildrenTokens which seems to be what I want, but I cannot get at that in my code.
I also tried using resourceList as a DataSet as per their example but it throws an exception.
Can someone see what I am doing wrong..... I am not familiar with parsing JSON in C#
You can use Json.Linq for that and parse a response into JObject, then iterate it foreach loop. It's possible, since Details is an array and JObject implements IDictionary<string, JToken> and IEnumerable<KeyValuePair<string, JToken>>
var jObject = JObject.Parse(response);
foreach (var detail in jObject["operation"]["Details"])
{
var description = detail["RESOURCE DESCRIPTION"].Value<string>();
//other properties
}
Here's an example using the JObject class instead of dynamic
JObject json = JObject.Parse(response);
string status = json["operation"]["result"]["status"].Value<string>();
foreach (JToken resource in json["operation"]["Details"])
{
string id = resource["RESOURCE ID"].Value<string>();
string name = resource["RESOURCE NAME"].Value<string>();
}
It is as simple as this:
Your Model classes would look like:
public class Result
{
public string status { get; set; }
public string message { get; set; }
}
public class Detail
{
[JsonProperty("RESOURCE DESCRIPTION")]
public string ResourceDescription { get; set; }
[JsonProperty("RESOURCE TYPE")]
public string ResourceType { get; set; }
[JsonProperty("RESOURCE ID")]
public string ResourceId { get; set; }
[JsonProperty("RESOURCE NAME")]
public string ResourceName { get; set; }
[JsonProperty("NOOFACCOUNTS")]
public string NoOfAccounts { get; set; }
}
public class Operation
{
public string name { get; set; }
public Result result { get; set; }
public int totalRows { get; set; }
public List<Detail> Details { get; set; }
}
public class RootObject
{
public Operation operation { get; set; }
}
To de-serialize:
var json = JsonConvert.DeserializeObject<RootObject>(response);
To access a property:
var name=json.operation.name
To access your Details:
foreach(var item in json.operation.Details)
{
var myresourcename=item.ResourceName;
//So on
}
I am coming to a problem where I am returning a whole guid from my rest api, however I just want to get the get the WelcomeMessage to show dynamically instead of hardcoding it. can anyone help me solve this issue. thanks for the help.
I have a child name Tests above the guid
Json:
{
"42f6be79-443b-4845-8549-865af9e74988": {
"Active": true,
"CompletedMessage": "Placeholder",
"CreatedBy": "",
"Description": "Placeholder",
"DisplayName": "Placeholder1",
"ID": "be193200-c277-48bd-90ab-796e869f2e0b",
"QuestionsIDs": [
"bd341962-6c7f-459d-88ea-86aa7186840a",
"bd341962-6c7f-459d-88ea-86aa7186840a"
],
"WelcomeMessage": "Placeholder3"
}
}
Code:
public Text welcomeMessage;
private async void welcomeMessage()
{
Dictionary<string, Questions> questionDictionary = new Dictionary<string, Questions>();
string json = #"{https://PROJECT_URL.firebaseio.com/Tests/WelcomeMessage.json";
questionDictionary = JsonConvert.DeserializeObject<Dictionary<string, Questions>>(json);
foreach (Questions question in questionDictionary.Values)
{
Guid[] guids = question.QuestionsIDs;
string welcomeMessage = question.WelcomeMessage;
welcomeMessageShown = GetComponent<Text>();
welcomeMessageShown.text = welcomeMessage.ToString();
}
First create a class that matches your object. Lets say Question
public partial class Question
{
[JsonProperty("Active")]
public bool Active { get; set; }
[JsonProperty("CompletedMessage")]
public string CompletedMessage { get; set; }
[JsonProperty("CreatedBy")]
public string CreatedBy { get; set; }
[JsonProperty("Description")]
public string Description { get; set; }
[JsonProperty("DisplayName")]
public string DisplayName { get; set; }
[JsonProperty("ID")]
public Guid Id { get; set; }
[JsonProperty("QuestionsIDs")]
public Guid[] QuestionsIDs { get; set; }
[JsonProperty("WelcomeMessage")]
public string WelcomeMessage { get; set; }
}
Since our json is a key value pair we need to deserialize it to a Dictionary
Our dictionary definition will be
Dictionary<string, Question> questionDictionary = new Dictionary<string, Question>();
Now using Newtonsoft.Json we can deserialize this to our dictionary.
string json = #"{
'42f6be79-443b-4845-8549-865af9e74988': {
'Active': true,
'CompletedMessage': 'Placeholder',
'CreatedBy': '',
'Description': 'Placeholder',
'DisplayName': 'Placeholder1',
'ID': 'be193200-c277-48bd-90ab-796e869f2e0b',
'QuestionsIDs': [
'bd341962-6c7f-459d-88ea-86aa7186840a',
'bd341962-6c7f-459d-88ea-86aa7186840a'
],
'WelcomeMessage': 'Placeholder3'
}
}";
Dictionary<string, Question> questionDictionary = JsonConvert.DeserializeObject<Dictionary<string, Question>>(json);
foreach (Question question in questionDictionary.Values)
{
Guid[] guids = question.QuestionsIDs; // Do whatever you want with it
string welcomeMessage = question.WelcomeMessage;
}
Finding the right title for this problem was kinda hard so I'll try to explain the problem a bit better below.
I am making a call to an API which returns the following JSON object:
{{
"id": "jsonrpc",
"jsonrpc": "2.0",
"result": {
"result": [
{
"AccountId": 285929,
"Flags": [
"Managed_Obsolete"
],
"PartnerId": 73560,
"Settings": [
{
"AN": "company_1"
},
{
"CD": "1435323320"
},
{
"ED": "2147483647"
},
{
"OS": "Windows Server 2012 R2 Standard Edition (9600), 64-bit"
},
{
"OT": "2"
},
{
"T3": "1085792125772"
},
{
"US": "958222150780"
},
{
"YS": "100"
}
]
},
{
"AccountId": 610474,
"Flags": null,
"PartnerId": 249262,
"Settings": [
{
"AN": "company_2"
},
{
"CD": "1522143635"
},
{
"ED": "2147483647"
},
{
"OS": "Windows 7 Professional Service Pack 1 (7601), 64-bit"
},
{
"OT": "2"
},
{
"T3": "598346102236"
},
{
"US": "758149148249"
},
{
"YS": "100"
}
]
},
],
"totalStatistics": null
},
}}
In above result I listed only the first 2 accounts (total of 80+ accounts normally).
Deserializing the object works fine, I am putting the JSON object fields inside my C# model (list).
The problem however is that I can't get the (inner) Settings array properly in my model. The settings array keys are unknown, I define these keys when I call the API:
JObject requestObject = new JObject();
requestObject.Add(new JProperty("id", "jsonrpc"));
requestObject.Add(new JProperty("jsonrpc", "2.0"));
requestObject.Add(new JProperty("method", "myMethod"));
requestObject.Add(new JProperty("visa", someID));
requestObject.Add(new JProperty("params",
new JObject(
new JProperty("query", new JObject(
new JProperty("PartnerId", partnerId),
new JProperty("StartRecordNumber", 0),
new JProperty("RecordsCount", 9999999),
new JProperty("Columns", new JArray("AR", "AN", "US", "T3", "OT", "OS", "YS"))
)),
new JProperty("timeslice", unixDate),
new JProperty("totalStatistics", "*")
))
);
In above call I define the keys for the Settings array, this could however also be just one key or more. For this reason I want to make my Settings property in my C# model generic (I don't want to list all the possible key names because this are over 100 keys).
What I had so far:
List<EnumerateAccountHistoryStatisticsResult> resultList = new List<EnumerateAccountHistoryStatisticsResult>();
var result = JsonConvert.DeserializeObject<JObject>(streamreader.ReadToEnd());
dynamic innerResult = result["result"]["result"];
foreach (var obj in innerResult)
{
resultList.Add(
new EnumerateAccountHistoryStatisticsResult
{
AccountId = obj.AccountId,
Flags = obj.Flags.ToObject<IEnumerable<string>>(),
PartnerId = obj.PartnerId,
Settings = obj.Settings.ToObject<List<ColumnSettingsResult>>(),
});
}
The EnumerateAccountHistoryStatisticsResult Model:
public class EnumerateAccountHistoryStatisticsResult
{
public int AccountId { get; set; }
public IEnumerable<string> Flags { get; set; }
public int PartnerId { get; set; }
public List<ColumnSettingsResult> Settings { get; set; }
}
The ColumnSettingsResult model:
public class ColumnSettingsResult
{
public string AR { get; set; }
public string AN { get; set; }
public string US { get; set; }
public string T3 { get; set; }
public string OT { get; set; }
public string OS { get; set; }
public string YS { get; set; }
// and list all other columns...
}
With above models I would need to list all the possible columns which are over 100 properties, besides that the result of the Settings list is not logical because I get all the property values but for each different key I get null values:
The ColumnSettingsResult model should more be something like:
public class ColumnSettingsResult
{
public string ColumnName { get; set; }
public string ColumnValue { get; set; }
}
I cant get the key and value inside these two properties though without defining the key name inside the model..
I already tried several things without result (links below as reference).
Anyone that can get me in the right direction?
C# deserialize Json unknown keys
Convert JObject into Dictionary<string, object>. Is it possible?
Convert Newtonsoft.Json.Linq.JArray to a list of specific object type
Try making Settings of type Dictionary<string,string> (or List<KeyValuePair<string,string>> if Dictionary doesn't give you what you want.
public class MyJsonObject
{
public string id { get; set; }
public string jsonrpc { get; set; }
public Result result { get; set; }
public class Result2
{
public int AccountId { get; set; }
public List<string> Flags { get; set; }
public int PartnerId { get; set; }
public Dictionary<string,string> Settings { get; set; } //or List<KeyValuePair<string,string>>
}
public class Result
{
public List<Result2> result { get; set; }
public object totalStatistics { get; set; }
}
}
Then JsonConvert.DerserializeObject<MyJsonObject>(jsonString);
I am familiar with JSON.net a bit and can Deserialize the JSON with basic structure (upto one child). I am currently in process of Deserializing the JSON that is returned from Netatmo API. The structure of JSON is complicated for me. Following is the basic structure of the JSON,
_id
place
location
Dynamic Value 1
Dynamic Value2
altitude
timezone
mark
measures
Dynamic Value 1
res
Dynamic Value 1
Dynamic Value 1
Dynamic Value 2
type
Dynamic Value 1
Dynamic Value 2
modules
Dynamic Value 1
Dynamic Value 1 and Dynamic Value 2 represents the values that is changed for each id. The complete JSON is given below,
{
"body": [{
"_id": "70:ee:50:02:b4:8c",
"place": {
"location": [-35.174779762001, -5.8918476117544],
"altitude": 52,
"timezone": "America\/Fortaleza"
},
"mark": 0,
"measures": {
"02:00:00:02:ba:2c": {
"res": {
"1464014579": [16.7, 77]
},
"type": ["temperature", "humidity"]
},
"70:ee:50:02:b4:8c": {
"res": {
"1464014622": [1018.1]
},
"type": ["pressure"]
}
},
"modules": ["02:00:00:02:ba:2c"]
}, {
"_id": "70:ee:50:12:40:cc",
"place": {
"location": [-16.074257294385, 11.135715243973],
"altitude": 14,
"timezone": "Africa\/Bissau"
},
"mark": 14,
"measures": {
"02:00:00:06:7b:c8": {
"res": {
"1464015073": [26.6, 78]
},
"type": ["temperature", "humidity"]
},
"70:ee:50:12:40:cc": {
"res": {
"1464015117": [997]
},
"type": ["pressure"]
}
},
"modules": ["02:00:00:06:7b:c8"]
}],
"status": "ok",
"time_exec": 0.010364055633545,
"time_server": 1464015560
}
I am confused by looking at the complex structure of this JSON. For single level of JSON I have used this code in the past,
IList<lstJsonAttributes> lstSearchResults = new List<lstJsonAttributes>();
foreach (JToken objResult in objResults) {
lstJsonAttributes objSearchResult = JsonConvert.DeserializeObject<lstJsonAttributes>(objResult.ToString());
lstSearchResults.Add(objSearchResult);
}
But for so many child I have yet to understand how the object class will be created. Any guidance will highly appreciated.
Update:
This is what I have achieved so far.
I have created a main class as below,
public class PublicDataClass
{
public string _id { get; set; }
public PublicData_Place place { get; set; }
public string mark { get; set; }
public List<string> modules { get; set; }
}
and "Place" class is as follow,
public class PublicData_Place
{
public List<string> location { get; set; }
public string altitude { get; set; }
public string timezone { get; set; }
}
Then I have Deserialized the object in the following code line,
var obj = JsonConvert.DeserializeObject<List<PublicDataClass>>(jsonString);
I can now successfully get all the data except the "measures" which is little bit more complicated.
Using json.net, JSON objects that have arbitrary property names but fixed schemas for their values can be deserialized as a Dictionary<string, T> for an appropriate type T. See Deserialize a Dictionary for details. Thus your "measures" and "res" objects can be modeled as dictionaries.
You also need a root object to encapsulate your List<PublicDataClass>, since your root JSON container is an object like so: { "body": [{ ... }] }.
Thus you can define your classes as follows:
public class RootObject
{
public List<PublicDataClass> body { get; set; }
public string status { get; set; }
public double time_exec { get; set; }
public int time_server { get; set; }
}
public class PublicDataClass
{
public string _id { get; set; }
public PublicData_Place place { get; set; }
public int mark { get; set; }
public List<string> modules { get; set; }
public Dictionary<string, Measure> measures { get; set; }
}
public class PublicData_Place
{
public List<double> location { get; set; } // Changed from string to double
public double altitude { get; set; } // Changed from string to double
public string timezone { get; set; }
}
public class Measure
{
public Measure()
{
this.Results = new Dictionary<string, List<double>>();
this.Types = new List<string>();
}
[JsonProperty("res")]
public Dictionary<string, List<double>> Results { get; set; }
[JsonProperty("type")]
public List<string> Types { get; set; }
}
Then do
var root = JsonConvert.DeserializeObject<RootObject>(jsonString);
var obj = root.body;
I've worked with XML for a few years and my change to JSON structure I've got a little confused too, always that I want to see how an object look like I use this web site jsoneditoronline Just copy and paste your JSON and click on arrow to parse to an object, I hope it helps until you get used to JSON structure.
This is my json string :
{"loginAccounts": [
{
"name": "abc",
"accountId": "123456",
"baseUrl": "https://abc.defghi.com/test/t12/remark/123456",
"isDefault": "true",
"userName": "Ceilina James",
"userId": "7c5bat57-850a-5c93-39eb-2015ai9o8822",
"email": "abc#test.com",
"siteDescription": ""
}
]}
I need "baseUrl" value. How to find it in the C# ?
You could use a JSON serializer such as the JavaScriptSerializer class to deserialize this JSON string to a C# class and then extract the required value. For example you could have the following model:
public class SomeModel
{
public LoginAccount[] LoginAccounts { get; set; }
}
public class LoginAccount
{
public string Name { get; set; }
public string AccountId { get; set; }
public string BaseUrl { get; set; }
public string IsDefault { get; set; }
...
}
and then:
string json = "... your JSON string comes here ...";
var serializer = new JavaScriptSerializer();
string json = ...
var model = (SomeModel)serializer.Deserialize(json);
foreach (var account in model.LoginAccounts)
{
string baseUrl = account.BaseUrl;
...
}
Using Json.Net
foreach (var acc in JObject.Parse(json)["loginAccounts"])
{
Console.WriteLine(acc["baseUrl"]);
}