I want to send Post Request with C# WebClient with this Json Schema :
[
{
"id": "00000000-0000-0000-0000-000000000000",
"points": [
{
"timestamp": "2017-12-04T16:07:44.562Z",
"value": 0
}
]
}
]
I've tried This :
public class RequestData
{
public string id {get; set; }
public points points { get; set; }
}
public class points
{
public DateTime timestamp { get; set; }
public float value { get; set; }
}
My Program :
Random number = new Random();
var req = new RequestData();
req.id = "0e13d9c-571c-44f4-b796-7c40c0e20a1d";
req.points = new points { timestamp = DateTime.UtcNow, value =
number.Next(100, 99999) };
JsonSerializerSettings settings = new JsonSerializerSettings();
var data = JsonConvert.SerializeObject(req);
WebClient client = new WebClient();
client.Headers.Add(HttpRequestHeader.Authorization,
AquaAtuhorization.accessToken);
client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
client.UploadString ("http://localhost:8080/api/v1/data/writeNumericValues",
data );
And I always get Http 415 (Unsupported Media Type).
How could I format my C# Object as the restApi accepeted Format.
Look at the JSON, the square brackets [ ] denote that something is an array. In this case both RequestData and points have to be an array, see the example below:
public class RequestData
{
public string id { get; set; }
public List<points> points { get; set; } // I use list, could be an array
}
public class points
{
public DateTime timestamp { get; set; }
public float value { get; set; }
}
Then construct your req object like this:
var req = new List<RequestData> // Again I use list, could be an array
{
new RequestData
{
id = "0e740d9c-571c-44f4-b796-7c40c0e20a1d",
points = new List<points> // Defined as a list, even though it has one entry
{
new points
{
timestamp = DateTime.UtcNow,
value = number.Next(100, 99999)
}
}
}
};
Then just serialize it as normal, the result will be the below:
[
{
"id":"0e740d9c-571c-44f4-b796-7c40c0e20a1d",
"points":[
{
"timestamp":"2017-12-04T17:12:25.8957648Z",
"value":59522.0
}
]
}
]
Your Json class needs to be like this, see http://json2csharp.com/ or use paste as JSON from VS https://blog.codeinside.eu/2014/09/08/Visual-Studio-2013-Paste-Special-JSON-And-Xml/
public class Point
{
public DateTime timestamp { get; set; }
public int value { get; set; }
}
public class RootObject
{
public string id { get; set; }
public List<Point> points { get; set; }
}`
Related
I'm getting a post request to my api (x-www-form-urlencoded) and the body of the request looks like this:
worker=%7B%22_id%22%3A+%7B%22%24oid%22%3A+%2261asd23e9231241dfd2b4c3bd%22%7D%2C+%22sid%22%3A+%22WKb32df49cas43413585352e8a6e2%cd%22%22%%22%3A+1234154123%7D%7D&task=%7B%22_id%22%3A+%7B%22%24oid%22%3A+%2261caffc34dsf33182b4c789
continues.
There are 2 objects (classes) that I need to receive in this incoming request, and I created the class structure of these 2 objects: For example, my class structure is as follows:
public class Worker
{
[JsonProperty("friendly_name")]
public string FriendlyName { get; set; }
[JsonProperty("date_updated")]
public WorkerDateUpdated DateUpdated { get; set; }
[JsonProperty("activity")]
public string Activity { get; set; }
[JsonProperty("workspace_sid")]
public string WorkspaceSid { get; set; }
[JsonProperty("date_created")]
public WorkerDateCreated DateCreated { get; set; }
[JsonProperty("queues")]
public List<string> queues { get; set; }
}
public class Task
{
[JsonProperty("reason")]
public string Reason { get; set; }
[JsonProperty("date_updated")]
public TaskDateUpdated DateUpdated { get; }
[JsonProperty("assignment_status")]
public string AssignmentStatus { get; set; }
[JsonProperty("total_cost")]
public TaskTotalCost TotalCost { get; set; }
}
In the incoming request, I receive 3 objects (class) as url-encoded, I only need 2 objects and their properties.
using (var reader = new StreamReader(
HttpContext.Request.Body,
encoding: Encoding.UTF8,
detectEncodingFromByteOrderMarks: false
))
{
var bodyString = await reader.ReadToEndAsync();
_logger.LogInformation("BodyString ---> " + bodyString);
var decodedUrl = HttpUtility.UrlDecode(bodyString);
_logger.LogInformation(" decodedUrl ---> " + decodedUrl);
}
I can read the incoming body and convert it to decoded format. Below is an example:
worker={"_id": {"$oid": "XXXXXXXXXX"}, "sid": "XXXXXXXXXXXXX", "x": true, "account_sid": "XXXXXXXXXXXX", "workspace_sid": "XXXXXXXXXXXX", "queues ": ["XXXXXXXXXXXXXX"], "activity": "idle", "available": true, "friendly_name": "XXXXXXXX", "attributes": {"mail": "XXXXXXXXXXXX", "name": "XXXXXXXXXX" }, "date_created": {"$date": XXXXXXXXX}, "date_updated": {"$date": XXXXXXXXXX}, "date_status_changed": {"$date": XXXXXXXXXXXXX}}&task={"_id": {" $oid": "XXXXXXXXXXXXXX"}, "sid": "XXXXXXXXXXXX", "x": true, "account_sid": "XXXXXXXXXXX", "workspace_sid": "XXXXXXXXXXXXXXXXXX", "workflow_sid": "XXXXXXXXXXXX", "workflow_friendly_name" : "daytime1", "initial_attributes": {"station_name": "XXXXX", "component_type": X, "component_id": XXX, "mail": "XXXXXX", "main_issue": "XXXXXXXXXXXXXX", "predictivi_maintenance_time": "XXXXXXXXXX", "hospital_name": "\u00dcsk\u00fcdar XXXXXXXXXXX"}
I can see it as , but I can't deserialize it. Or I don't know if I'm doing it wrong. I have created a separate class that contains my 2 classes. I keep my Worker and Task class in it, I cannot deserialize to that class, it does not deserialize in any way. Unexpected charachter throws exception. How can I convert these objects to json format or object format?
Edit:
My Other Custom classes:
public class TaskDateUpdated
{
[JsonProperty("$date")]
public long Date { get; set; }
}
public class TaskTotalCost
{
[JsonProperty("$numberDecimal")]
public string NumberDecimal { get; set; }
}
public class TaskDateCreated
{
[JsonProperty("$date")]
public long Date { get; set; }
}
public class TaskLastChargeDate
{
[JsonProperty("$date")]
public long Date { get; set; }
}
public class TaskId
{
[JsonProperty("$oid")]
public string Oid { get; set; }
}
public class WorkerDateUpdated
{
[JsonProperty("$date")]
public long date { get; set; }
}
public class WorkerDateCreated
{
[JsonProperty("$date")]
public long date { get; set; }
}
public class WorkerDateStatusChanged
{
[JsonProperty("$date")]
public long date { get; set; }
}
I also have a single class containing these 2 classes, I get an error when I try to deserialize to this class, I also get an error when I try to deserialize it to other worker and task classes separately. I can't deserialize at all.
public class DataContainer
{
public Task Task { get; set; }
public Worker Worker { get; set; }
}
My post method looks like this:
[HttpPost]
[Consumes("application/x-www-form-urlencoded")]
public async Task<ActionResult<ResponseRequest>> AddWorkerTask()
{
using (var reader = new StreamReader(
HttpContext.Request.Body,
encoding: Encoding.UTF8,
detectEncodingFromByteOrderMarks: false
))
{
var bodyString = await reader.ReadToEndAsync();
_logger.LogInformation("BodyString ---> " + bodyString);
var decoded = HttpUtility.UrlDecode(bodyString);
//here is where i need to deserialize and convert it to
//a valid json and object
}
}
You can use HttpUtility.ParseQueryString to parse it into a NameValueCollection, then simply use the indexer ["worker"] on that.
[HttpPost]
[Consumes("application/x-www-form-urlencoded")]
public async Task<ActionResult<ResponseRequest>> AddWorkerTask()
{
using (var reader = new StreamReader(
HttpContext.Request.Body,
encoding: Encoding.UTF8,
detectEncodingFromByteOrderMarks: false
))
{
var bodyString = await reader.ReadToEndAsync();
var decoded = HttpUtility.ParseQueryString(bodyString);
var worker = JsonSerializer.Deserialize<Worker>(decoded["worker"]);
var task = JsonSerializer.Deserialize<Task>(decoded["task"]);
// or whatever your JSON deserializer is
}
}
In newer versions of ASP.net Core you can use HttpContext.Request.Form
[HttpPost]
[Consumes("application/x-www-form-urlencoded")]
public async Task<ActionResult<ResponseRequest>> AddWorkerTask()
{
var worker = JsonSerializer.Deserialize<Worker>(HttpContext.Request.Form["worker"]);
var task = JsonSerializer.Deserialize<Task>(HttpContext.Request.Form["task"]); // or whatever your JSON deserializer is
}
I created a class for a json object (not automatically).
However, I don't quite understand how to make the button object contain nameless arrays as in the example?
Example JSON (what the server expects to receive):
{
"one_time":false,
"buttons":[
[
{
"action":{
"type":"location",
"payload":"{\"button\": \"1\"}"
}
}
],
[
{
"action":{
"type":"open_app",
"app_id":6232540,
"owner_id":-157525928,
"hash":"123",
"label":"LiveWidget"
}
}
],
[
{
"action":{
"type":"vkpay",
"hash":"action=transfer-to-group&group_id=181108510&aid=10"
}
}
],
[
{
"action":{
"type":"text",
"payload":"{\"button\": \"1\"}",
"label":"Red"
},
"color":"negative"
},
{
"action":{
"type":"text",
"payload":"{\"button\": \"2\"}",
"label":"Green"
},
"color":"positive"
},
{
"action":{
"type":"text",
"payload":"{\"button\": \"2\"}",
"label":"Blue"
},
"color":"primary"
},
{
"action":{
"type":"text",
"payload":"{\"button\": \"2\"}",
"label":"White"
},
"color":"secondary"
}
]
]
}
Keyboard.cs (manually created class)
public class Keyboard
{
public bool one_time { get; set; }
public List<buttons> buttons { get; set; }
}
public class buttons
{
public action action { get; set; }
public string color { get; set; }
}
public class action
{
public string type { get; set; }
public string payload { get; set; }
public string label { get; set; }
}
I create an object like this:
var Keyboard_obj = new Keyboard()
{
one_time = false,
buttons = new List<buttons>()
{
new buttons()
{
action = new action()
{
type = "test1",
label = "class obj",
payload = "{\"button\": \"1\"}"
},
color = "negative"
},
new buttons()
{
action = new action()
{
type = "test2",
label = "class obj",
payload = "{\"button\": \"2\"}"
},
color = "positive"
}
}
};
How can I remake the class so that I get a JSON object like the example above?
You should use JsonSerializer
this code will give you a Json object like the example above:
var output = JsonConvert.SerializeObject(Keyboard_obj );
you can also create an object from Json:
Keyboard keyboard = JsonConvert.DeserializeObject<Keyboard>(output );
I can suggest you to try using dynamic objects
dynamic myObject = JsonConvert.DeserializeObject<dynamic>(input);
decimal Amount = Convert.ToDecimal(myObject.Amount);
string Message = myObject.Message;
If you take your JSON and use Visual studio (use paste as special option) or online tool to generate classes from the provided JSON then following classes will be generated.
Note - RootBoject.buttons Is an array or array instead of single collection.
public class Rootobject
{
public bool one_time { get; set; }
public Button[][] buttons { get; set; }
}
public class Button
{
public Action action { get; set; }
public string color { get; set; }
}
public class Action
{
public string type { get; set; }
public string payload { get; set; }
public int app_id { get; set; }
public int owner_id { get; set; }
public string hash { get; set; }
public string label { get; set; }
}
So with the provided class schema if you use JsonConvert to searialize the RootObject then you will get the required JSON.
What would be my next step to take? I want to be able to write the block with the id value of ex 1. Or the block with ex GPIO value 3 with something simple as maybe WriteLine(id1) Relay.cs
public class Relay
{
public int GPIO { get; set; }
public int id { get; set; }
public int status { get; set; }
public string type { get; set; }
}
Program.cs
static void Main(string[] args)
{
var client = new RestClient("http://192.168.0.3:1337/auto/api/v1.0/");
var request = new RestRequest("relays", Method.GET);
request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
var response = client.Execute<Relay>(request);
Console.WriteLine(response.Content);
Console.ReadLine();
}
and my array on 192.168.0.3:1337/auto/api/v1.0/relays
{
"relays": [
{
"GPIO": 2,
"id": 1,
"status": 0,
"type": "Relay"
},
{
"GPIO": 3,
"id": 2,
"status": 0,
"type": "Relay"
}
]
}
I'm sorry if anything is unclear, or if the answer is simple. If I missed to include something important, just point it out and I'll post it!
you can deserialize it in List of Relay and iterate and read any value you want
static void Main(string[] args)
{
var client = new RestClient("http://192.168.0.3:1337/auto/api/v1.0/");
var request = new RestRequest("relays", Method.GET);
request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
var response = client.Execute<Relay>(request);
JavaScriptSerializer ser = new JavaScriptSerializer();
var relayList = ser.Deserialize<List<Relay>>(response.Content);
foreach(Relay relay in relayList)
Console.WriteLine(relay.ID);
Console.ReadLine();
}
You need to parse that JSON into objects to manipulate them, and it seems that your REST client already does it, just need to pass the correct type.
1-Create the class structure like the structure you're receiving:
public class Relay
{
public int GPIO { get; set; }
public int id { get; set; }
public int status { get; set; }
public string type { get; set; }
}
public class RelayCollection
{
public Relay[] relays { get; set; }
}
2-Parse the received json:
var relayCollection = client.Execute<RelayCollection>(request);
Now you have all the relays inside relayCollection.relays, manipulate them as any other array/class
Beforehand I apologize for my English, but you can serialize a json by creating a list of your class and then deserialize the json like this:
public class Relay {
int GPIO;
public int gPIO {get {return GPIO;} set {GPIO=value;}}
int Id;
public int ID {get {return Id;} set {Id = value;}}
int Status;
public int status {get {return Status;} set {Status = value;}}
string Type;
public string type {get {return Type;} set {Type = value;}}
}
Now create a class list
List<Relay > relayList = new List<Relay >();
And finally deserealize the json
relayList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Relay>>(request);
How can I deserialize JSON to a list using DeserializeObject and JsonProperty while the indexes of the elements are non-integer?
JSON:
{
"success": true,
"TEST": {
"360450323_188530139": {
"id": "24216",
"name": "zxc",
"desc": "cxz"
},
"310777518_0": {
"id": "6458678634",
"name": "dfgsfd",
"desc": "sdfxcvnbhr"
}
}
}
Is there any way to make a list from that?
I've tried:
using(WebClient wc = new WebClient())
{
var url = "...";
var json = wc.DownloadString(url);
Result result = JsonConvert.DeserializeObject<Result>(json);
}
public class Result
{
[JsonProperty("success")]
public string Success { get; set; }
[JsonProperty("TEST")]
public List<Test> Tests{ get; set; }
}
public class Test
{
[JsonProperty("id")]
public int id { get; set;}
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("desc")]
public string Desc { get; set; }
}
You have a keyed collection in the JSON, which maps easily to a dictionary. You could examine result.Test.Values
using(WebClient wc = new WebClient())
{
var url = "...";
var json = wc.DownloadString(url);
Result result = JsonConvert.DeserializeObject<Result>(json);
// result.Values should contain the Test instances at this point.
}
public class Result
{
[JsonProperty("success")]
public string Success { get; set; }
[JsonProperty("TEST")]
public Dictionary<string,Test> Tests{ get; set; }
}
public class Test
{
// omitted - same as in question.
}
This question already has answers here:
How can I parse a JSON string that would cause illegal C# identifiers?
(3 answers)
Closed 8 years ago.
I am trying to deserialize a Json response from an API.
The data looks like this
{
"response": {
"6112": {
"ID": 6112,
"Title": "AdditionalPhotos"
},
"5982": {
"ID": 5982,
"Title": "BikeRide"
},
"total_records": "20",
"returned_count": 10,
"returned_records": "1-10"
}
}
C# class:
public class Products
{
public class Product
{
public string Id { get; set; }
public string Title { get; set; }
}
public Product product { get; set; }
}
public class ss
{
public Dictionary<string, Products.Product> Response { get; set; }
public string total_records { get; set; }
}
Serialization code
ss res = Newtonsoft.Json.JsonConvert.DeserializeObject<ss>(jsonData());
I can get it to work without the total_records entry and below by deserializng to a Dictionary <string , Product>. But I cannot figure out how to get it to work. This is the error I get
Error converting value "20" to type 'Products+Product'. Path 'response.total_records'
I know why I get the error, but I'm unsure how I can proceed without going in and substringing from total_records down. I have no control over the API data.
Edit: you guys are fast, I was still getting to putting the classes up
First you json is not valid one, it should look like this
{
"response":{
"6112":{
"ID":"6112",
"Title":"Additional Photos",
},
"5982":{
"ID":"5982",
"Title":"Bike Ride",
},
"total_records": "20",
"returned_count": "10",
"returned_records": "1-10",
}
}
If you mean the response to contain list it should look like this
{
"response":{
"myArray": [
{
"ID":"6112",
"Title":"Additional Photos",
},
{
"ID":"5982",
"Title":"Bike Ride",
}
],
"total_records": "20",
"returned_count": "10",
"returned_records": "1-10",
}
}
So your code look like this
public class MyArray
{
public string ID { get; set; }
public string Title { get; set; }
}
public class Response
{
public List<MyArray> myArray { get; set; }
public string total_records { get; set; }
public string returned_count { get; set; }
public string returned_records { get; set; }
}
public class RootObject
{
public Response response { get; set; }
}
If you have control over API response then please refer to Mzf's answer.
If you don't have control over API then it may not be possible to do this particular deserialization on one go. You might have to loop.
Here's my take.
Update
Modified my approach:
Created a class Response which inherits from Dictionary<string, Product>, and added the metadata parts like total_records, records_count to it's public properties. And created a JsonConverter that can deserialize JObject to Response class.
The logic used for deserialization is quite simple:
Extract the metadata parts like total_records, records_count to variables.
Then remove those metadata from the JObject, so that the key values becomes homogeneous.
Now Json.net will be easily able to serialize JObject to Response object, as key values are homogenous.
Assign the metadata extracted previously to the properties of Response object
public void Deserialize()
{
var json = #"{
'response':{
'6112':{
'ID':6112,
'Title':'Additional Photos',
},
'5982':{
'ID':5982,
'Title':'Bike Ride',
},
'total_records': '20',
'returned_count': 10,
'returned_records': '1-10',
}
}";
var responseObj = Newtonsoft.Json.JsonConvert.DeserializeObject<ss>(json, new ResponseConverter());
}
public class Response : Dictionary<string, Product>
{
public int total_records { get; set; }
public int returned_count { get; set; }
public string returned_records { get; set; }
}
public class Product
{
public string Id { get; set; }
public string Title { get; set; }
}
public class ss
{
public Response Response { get; set; }
}
public class ResponseConverter : Newtonsoft.Json.JsonConverter
{
private Response CreateResponse(Newtonsoft.Json.Linq.JObject jObject)
{
//preserve metadata values into variables
int total_records = jObject["total_records"].ToObject<int>();
var returned_records = jObject["returned_records"].ToObject<string>();
var returned_count = jObject["returned_count"].ToObject<int>();
//remove the unwanted keys
jObject.Remove("total_records");
jObject.Remove("returned_records");
jObject.Remove("returned_count");
//once, the metadata keys are removed, json.net will be able to deserialize without problem
var response = jObject.ToObject<Response>();
//Assign back the metadata to response object
response.total_records = total_records;
response.returned_count = returned_count;
response.returned_records = returned_records;
//.. now person can be accessed like response['6112'], and
// metadata can be accessed like response.total_records
return response;
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof(Response);
}
public override object ReadJson(Newtonsoft.Json.JsonReader reader, Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
{
var jObject = Newtonsoft.Json.Linq.JObject.Load(reader);
Response target = CreateResponse(jObject);
serializer.Populate(jObject.CreateReader(), target);
return target;
}
public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
In my opinion this is how the JSON file should look like:
{
"response": {
"5982": {
"ID": 5982,
"Title": "BikeRide"
},
"6112": {
"ID": 6112,
"Title": "AdditionalPhotos"
},
"total_records": "20",
"returned_count": 10,
"returned_records": "1-10"
}
}
and this is how the class should look like
public class __invalid_type__5982
{
public int ID { get; set; }
public string Title { get; set; }
}
public class __invalid_type__6112
{
public int ID { get; set; }
public string Title { get; set; }
}
public class Response
{
public __invalid_type__5982 __invalid_name__5982 { get; set; }
public __invalid_type__6112 __invalid_name__6112 { get; set; }
public string total_records { get; set; }
public int returned_count { get; set; }
public string returned_records { get; set; }
}
public class RootObject
{
public Response response { get; set; }
}