Currently facing an issue when making a JSON string in C# and trying to send it through to a web API using the WebClient.
Currently I have a few methods, first off the one that works. Using POSTMAN sending the following dataset of text through to the API works correctly:
POSTMAN Dataset - This Works in POSTMAN
{ "record":
{
"form_id": "efe4f66f-b57c-4497-a370-25c0f3d8746a",
"status": "240",
"latitude": -82.638039,
"longitude": 27.770787,
"form_values":
{
"833b": "99999",
"683b": "9999999",
"fa37": "Testing",
"b2e3": "Testing"
}
}
}
In C# I am using a few different ways to construct this sting with little success.
C# List Method - Newtonsoft.Json.Serialization
The first is building a List and then using Newtonsoft.Json to play with it:
List<Parent> DataList = new List<Parent>();
List<Child> Form = new List<Child.formvalues>();
var Formvals = new Child.formvalues
{
ActionDescription = Row.ActionDescription,
ActionNotes = Row.ActionNotes,
ActionID = Row.ActionID,
AssetID = Row.AssetID
};
Form.Add(Formvals);
var DataElement = new Parent
{
form_id = AppFormID,
latitude = Lat,
longitude = Long,
status = Row.Status,
form_values = Form
};
DataList.Add(DataElement);
string json = JsonConvert.SerializeObject(DataList.ToArray());
This code results in the following string:
[
{\"form_id\":\"efe4f66f-b57c-4497-a370-25c0f3d8746a\",
\"latitude\":-82.638039,
\"longitude\":27.770787,
\"status\":239,
\"form_values\":[
{\"833b\":99999,
\"683b\":9999999,
\"fa37\":\"Testing\",
\"b2e3\":\"Testing\"
}]
}
]
Another attempt I am trying is the following, which in my opinion is the closest so far:
C# - String Building Method
string Content = #"{ "
+ "\"record\": {"
+ "\"form_id\": "\"" + AppFormID + ""\","
+ "\"status\": "\"" + Row.Status + ""\","
+ "\"latitude\": "+ Lat + ","
+ "\"longitude\": "+ Long + ","
+ "\"form_values\": {"
+ "\"833b\": "\""+Row.AssetID +""\","
+ "\"683b\": "\""+Row.ActionID + ""\","
+ "\"fa37\": "\""+Row.ActionDescription + ""\","
+ "\"b2e3\": "\""+Row.ActionNotes + ""\""
+ "}"
+ "}"
+ "}";
That line results in:
{ \"record\":
{
\"form_id\": \"efe4f66f-b57c-4497-a370-25c0f3d8746a\",
\"status\": \"239\",
\"latitude\": -82.638039,
\"longitude\": 27.770787,
\"form_values\":
{
\"833b\": \"99999\",
\"683b\": \"9999999\",
\"fa37\": \"Testing\",
\"b2e3\": \"Testing\"
}
}
}
The Question!
So the question, is someone able to help me achieving the format in the first JSON that I put into POSTMAN exactly?
UPDATE - 6:40PM
Web client code c#
AppURLRef is set in code further up and appears to be correct in the debugger. json is the result of the tests.
var http = new WebClient();
http.Headers.Add(HttpRequestHeader.ContentType, "application/json");
var response = http.UploadString(AppURLRef, "POST", json);
Result of Update
"[{\"record\":{\"form_id\":\"efe4f66f-b57c-4497-a370-25c0f3d8746a\",
\"latitude\":-82.638039,
\"longitude\":27.770787,
\"status\":\"240\",
\"form_values\":
[{\"833b\":\"99999\",
\"683b\":\"9999999\",
\"fa37\":\"Testing\",
\"b2e3\":\"Testing\"}]}}]"
Try the following.
public class Record
{
[JsonProperty(PropertyName = "form_id")]
public string FormId { get; set; }
[JsonProperty(PropertyName = "status")]
public string Status { get; set; }
[JsonProperty(PropertyName = "latitude")]
public decimal Latitude { get; set; }
[JsonProperty(PropertyName = "longitude")]
public decimal Longitude { get; set; }
[JsonProperty(PropertyName = "form_values")]
public Dictionary<string, string> FormValues { get; set; }
}
public class RecordContainer
{
[JsonProperty(PropertyName = "record")]
public Record Record { get; set; }
}
Usage:
var container = new RecordContainer();
container.Record = new Record();
// Code to populate values
JsonConvert.SerializeObject(container);
I am using Newtonsoft Json to serialize and I get the same output that you have asked for.
I'd try the following along with NewtonSoft.JSON.
var data = new
{
record = new
{
form_id = "efe4f66f-b57c-4497-a370-25c0f3d8746a",
status = "240",
latitude = -82.638039,
longitude = 27.770787,
form_values = new Dictionary<string, string>();
}
}
data.record.form_values["833b"] = "99999";
data.record.form_values["683b"] = "9999999";
data.record.form_values["fa37"] = "Testing";
data.record.form_values["b2e3"] = "Testing";
Then get the JSON:
string json = JsonConvert.SerializeObject(data);
I think it is much similar to this One. you can try the following code to achhieve the requirement:
var dd = {
"FirstName": "ABC",
"username": "abc123",
"password": "abc#123",
"Cnumbers": [{
"Home": "0987654321"
}, {
"Company": "7654321"
}]
}
Related
I need help extracting and returning values from json as either doubles or string, either should be fine.
The URL being used it: <https://earthquake.usgs.gov/ws/designmaps/asce7-16.json?latitude=34&longitude=-118&riskCategory=III&siteClass=C&title=Example>
here is the json
{
"request": {
"date": "2021-01-30T19:07:52.176Z",
"referenceDocument": "ASCE7-16",
"status": "success",
"url": "https://earthquake.usgs.gov/ws/designmaps/asce7-16.json?latitude=34&longitude=-118&riskCategory=III&siteClass=C&title=Example",
"parameters": {
"latitude": 34,
"longitude": -118,
"riskCategory": "III",
"siteClass": "C",
"title": "Example"
}
},
"response": {
"data": {
"pgauh": 0.819,
"pgad": 1.021,
"pga": 0.819,
"fpga": 1.2,
"pgam": 0.983,
"ssrt": 1.888,
"crs": 0.896,
"ssuh": 2.106,
"ssd": 2.432,
"ss": 1.888,
"fa": 1.2,
"sms": 2.265,
"sds": 1.51,
"sdcs": "D",
"s1rt": 0.669,
"cr1": 0.9,
"s1uh": 0.743,
"s1d": 0.963,
"s1": 0.669,
"fv": 1.4,
"sm1": 0.936,
"sd1": 0.624,
"sdc1": "D",
"sdc": "D",
"tl": 8,
"t-sub-l": 8,
"cv": 1.278,
...
url is defined as an input and Ss and S1 are defined as outputs per VisualStudio 2019 grasshopper developer C# template.
right now Ss and S1 return null values, they should return 1.888 and 0.669, respectively.
using Grasshopper.Kernel;
using System;
using System.Net;
using Newtonsoft.Json.Linq;
protected override void SolveInstance(IGH_DataAccess DA)
{
string url = "";
DA.GetData(0, ref url);
using (WebClient wc = new WebClient())
{
var json = wc.DownloadString(url);
JObject jObj = JObject.Parse(json); // Parse the object graph
string Ss = (string)jObj["ss"];
string S1 = (string)jObj["s1"];
//Functions I also tried
//var data = jObj["data"];
//foreach (var d in data) ;
//var Ss = d["ss"];
//double Ss = jObj.GetValue("ss").ToObject<double>();
//string Ss = jObj.GetValue("ss").Value<string>();
//string Ss = jObj.GetValue("ss").ToString();
//string Ss = jObj["ss"].ToString();
DA.SetData(0, Ss);
DA.SetData(1, S1);
}
}
The information you are looking for is nested in two levels, you have to access the response object then the data object, this should work:
var json = wc.DownloadString(url);
JObject jObj = JObject.Parse(json); // Parse the object graph
var data = jObj["response"]["data"];
var ss = data["ss"].ToObject<double>(); // or .ToString() if you want the string value
var s1 = data["s1"].ToObject<double>(); // or .ToString() if you want the string value
DA.SetData(0, ss);
DA.SetData(1, s1);
note: this code lacks null checks and error handling (try-catch block) for the sake of simplicity. But you need to add that in your code.
your data is null, is because your need Deserilize Json
using httpclientFactory
var httpclient = _httpClientFactory.CreateClient();
var responseDatas = await httpclient.GetAsync("https://earthquake.usgs.gov/ws/designmaps/asce7-16.json?latitude=34&longitude=-118&riskCategory=III&siteClass=C&title=Example");
if (responseDatas.IsSuccessStatusCode)
{
var responseDatasJson = await responseDatas .Content.ReadAsStringAsync();
var options = new JsonSerializerOptions() { PropertyNameCaseInsensitive = true };
var resultDataJson = JsonSerializer.Deserialize<Root>(responseDatasJson, options);
return (resultDataJson);
}
for convert json to c# class use this site Json to c# class
or use visual studio options
Edit-->Paste Special-->Paste Json As Classes
public class Data {
public double ss{ get; set; }
public double s1{ get; set; }
....your properties
}
public class Response {
public Data data { get; set; }
}
public class Root {
public Response response { get; set; }
}
I need to retrieve the name of every product from an API to a listbox in C# (Visual Studio) and I've managed to login sucessfully into the API. I was able to retrieve the data of each product serialized but when I try to deserialize it the result for each product comes like this: 'app1.Form1+Data'. How can I retrieve the name of each product correctly?
Code:
public class Data
{
public int id { get; set; }
public string name { get; set; }
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 1; i <= 20; i++)
{
string url = reqlbl.Text;
WebClient c = new WebClient();
String userName = "user";
String passWord = "pass";
string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(userName + ":" + passWord));
c.Headers[HttpRequestHeader.Authorization] = "Basic " + credentials;
var json = c.DownloadString(url + i.ToString());
Data result = JsonConvert.DeserializeObject<Data>(json);
listBox1.Items.Add(result);
}
}
Note: The variable 'i' represents a product. There are 20 products in total.
Here's an API example of product 1:
{"1":{"id":1,"name":"Product1"}}
The json is basically a dictionary, ie assuming it can return more than 1 product or something similar, or maybe its just lazily built api, who knows
Exmaple
var str = "{\"1\":{\"id\":1,\"name\":\"Product1\"}}";
var dataDict = JsonConvert.DeserializeObject<Dictionary<string, Data>>(str);
foreach (var data in dataDict.Values)
Console.WriteLine(data.id + ", " + data.name);
Output
1, Product1
Demo here
I am trying to split a string to get json object value - I have text values with numerous lines in the format:
new Car() { Id = 1, Year = 1926, Make = "Chrysler", Model = "Imperial", ImageUrl = "{"data":{"images":[{"thumb_url":"https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRPe4CygIW-MuZL5jl77wlgXXK5_ANyC9l1X4QqLizCOkaVAlRe","image_url":"http://imperialclub.org/Yr/1926/photos/Phaeton2Big.jpg","width":1632,"height":1032}]},"error_code":0,"error":false,"message":"1 images(s) available"}" },
new Car() { Id = 2, Year = 1950, Make = "Hillman", Model = "Minx Magnificent", ImageUrl = "{"data":{"images":[{"thumb_url":"https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcScVsGEeRBh6xZYXr6Gm35Sk5ecSlk_ax3qZmoGRAtBbZC8vJZ9","image_url":"http://i.ebayimg.com/images/g/gcIAAOSwKadXPeLs/s-l300.jpg","width":300,"height":225}]},"error_code":0,"error":false,"message":"1 images(s) available"}" },
new Car() { Id = 3, Year = 1954, Make = "Chevrolet", Model = "Corvette", ImageUrl = "{"data":{"images":[{"thumb_url":"https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcSdZntu4tgWrZrxwqeuKlteCP9vJGnqUlmNq5JF1bBCf-EJy5r8","image_url":"http://momentcar.com/images/chevrolet-corvette-1954-1.jpg","width":1000,"height":600}]},"error_code":0,"error":false,"message":"1 images(s) available"}" },
What I would really like is to get them in the format:
new Car() { Id = 1, Year = 1926, Make = "Chrysler", Model = "Imperial", ImageUrl = "https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcRPe4CygIW-MuZL5jl77wlgXXK5_ANyC9l1X4QqLizCOkaVAlRe" },
new Car() { Id = 2, Year = 1950, Make = "Hillman", Model = "Minx Magnificent", ImageUrl = "https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcScVsGEeRBh6xZYXr6Gm35Sk5ecSlk_ax3qZmoGRAtBbZC8vJZ9" },
new Car() { Id = 3, Year = 1954, Make = "Chevrolet", Model = "Corvette", ImageUrl = "https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcSdZntu4tgWrZrxwqeuKlteCP9vJGnqUlmNq5JF1bBCf-EJy5r8" },
I know I can use JObject.Parse(data); to parse the json value - but just tring to get to it is becoming a bit of a nightmare. Is there a better way of doing this?
What I have so far:
static void Main(string[] args)
{
using (StreamWriter writer = new StreamWriter(#"c:\Data\temp\output.txt")) // file to write to
{
using (StreamReader reader = new StreamReader(#"c:\Data\temp\test.txt")) //file to read from
{
string line;
while (reader.ReadLine() != null)
{
line = reader.ReadLine();
string[] words = JsonSplitString(line);
string json = words[1];
writer.WriteLine("{0}", json);
}
}
}
}
static string[] JsonSplitString(string data)
{
return data.Split(new string[] { "ImageUrl" }, StringSplitOptions.None);
}
However I am getting a NullReferenceException - even though a string is being passed in to the JsonSplitString method.
You are calling reader.Readline() twice: once for the comparison and then again inside your loop. You are actually skipping every other line. And what is probably happening is that you are reaching the end of your file and then calling reader.Readline() again, which is null. Try this instead:
line = reader.ReadLine();
while (line != null)
{
string[] words = JsonSplitString(line);
string json = words[1];
writer.WriteLine("{0}", json);
line = reader.ReadLine();
}
using System;
using Newtonsoft.Json.Linq;
namespace JsonExperiments
{
class Program
{
static void Main(string[] args)
{
ExecuteEmployeeSearch();
Console.ReadLine();
}
static void ExecuteEmployeeSearch()
{
// mockup JSON that would be returned from API
string sampleJson = "{\"results\":[" +
"{\"employeename\":\"name1\",\"employeesupervisor\":\"supervisor1\"}," +
"{\"employeename\":\"name2\",\"employeesupervisor\":\"supervisor1\"}," +
"{\"employeename\":\"name3\",\"employeesupervisor\":[\"supervisor1\",\"supervisor2\"]}" +
"]}";
// Parse JSON into dynamic object, convenient!
JObject results = JObject.Parse(sampleJson);
// Process each employee
foreach (var result in results["results"])
{
// this can be a string or null
string employeeName = (string)result["employeename"];
// this can be a string or array, how can we tell which it is
JToken supervisor = result["employeesupervisor"];
string supervisorName = "";
if (supervisor is JValue)
{
supervisorName = (string)supervisor;
}
else if (supervisor is JArray)
{
// can pick one, or flatten array to a string
supervisorName = (string)((JArray)supervisor).First;
}
Console.WriteLine("Employee: {0}, Supervisor: {1}", employeeName, supervisorName);
}
}
}
}
In C# I make a GET reqeust to get som JSON data. In this JSON data there is an array, which I want to return the elements in this data. I'm using Json.NET to try to achieve this.
Here is my C# code to get a property in the JSON file.
public static string sendRequest(string url, string method)
{
try
{
// Set reqiued information for api
var httpWebRequest = (HttpWebRequest)WebRequest.Create(main_url + url);
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.Headers.Add("Authorization", "Bearer " + Current_Access_Token);
httpWebRequest.Accept = "application/json;v=1";
httpWebRequest.Method = method;
// Get adn return responses
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
string respStr = new StreamReader(httpResponse.GetResponseStream()).ReadToEnd();
return respStr;
}
catch (Exception e)
{
MessageBox.Show("No response");
}
return "";
}
public static string GetUserName()
{
string resp = sendRequest("api.php/currentCustomer/", "GET");
JObject jObject = JObject.Parse(resp);
JToken Juser = jObject["response"];
string UserName = (string)Juser["data"]["customer"]["name"];
return UserName;
}
The code above is how I return the name of a customer under customer. But under customer there is an array, "active_digital_subscriptions". In this array there is 2 elements which I want to access and save in setting.setting. "active_digital_subscriptions" only contains string.
JSON data:
{
"response":{
"code":200,
"status":"success",
"data":{
"customer":{
"id":"109701",
"email":"ahjune152#one.com",
"name":"ahjune152",
"first_name":"Loc",
"last_name":"Dai Le",
"registration_date":"2015-06-17",
"last_login_date":"2015-11-05",
"is_newsletter_subscriber":"1",
"digital_password":"YOtI1cuzL18dO3i9T9CBVCiX3lQa3iIPhcWDgiwHxxI=",
"language":{
"id":"1",
"name":"American english",
"code":"en"
},
"currency":{
"id":"1",
"name":"Danske kroner",
"sign":"DKK",
"code":"DKK",
"number":"208"
},
"preferred_platforms":[
],
"active_digital_subscriptions":[
{
"plan_id":"246",
"plan_title":"VIP Partner"
}
]
}
}
}
}
You can use SelectToken and SelectTokens for this purpose. Both support JSONPath query syntax:
var userName = (string)Juser.SelectToken("data.customer.name");
var customersItems = Juser.SelectTokens("data.customer.customers_items[*]")
.Select(t => (string)t)
.ToArray();
[*] is a wildcard that selects all elements in the array.
Update
Given your revised JSON, you can get the active user and active subscriptions like so:
var userName = (string)Juser.SelectToken("data.customer.name");
var activeSubscriptions = Juser.SelectTokens("data.customer.active_digital_subscriptions[*]")
.Select(t => new { PlanId = (string)t["plan_id"], PlanTitle = (string)t["plan_title"] })
.ToArray();
Note that the active subscriptions are in an array, so it would appear possible that there could be more than one. If you know there is only one, you can do
var activeSubscription = activeSubscriptions.SingleOrDefault();
Here I am returning the subscriptions in an anonymous type but you could define an explicit class if you prefer:
public class Subscription
{
public string PlanId { get; set; }
public string PlanTitle { get; set; }
}
And then do:
var activeSubscriptions = Juser.SelectTokens("data.customer.active_digital_subscriptions[*]")
.Select(t => new Subscription { PlanId = (string)t["plan_id"], PlanTitle = (string)t["plan_title"] })
.ToArray();
You could try this -
var subs = (JArray)JObject.Parse(json)["response"]["data"]["customer"]["active_digital_subscriptions"];
for (var i = 0; i < subs.Count; ++i)
{
var id = ((JValue)subs[i]["plan_id"]).Value;
var title = ((JValue)subs[i]["plan_title"]).Value;
}
I want to use a string array stored in the web.config to easily change its values, this is in the format: full_w=670|small_w=100,q=low|tiny_h=30,c=true. Each template is split by the | (pipe) and then each of those sets comprises of a name (left of _) and its corresponding values (right of _), the values can be several and each separated by the , (comma). I think this possibly qualifies for a 3D array, I just can't seem to get an easy way to read this in a sensible manner. Any ideas or solutions as to the best way to read/manage the data from this string?
Basically, in the end I want to be able to call the template small and read its values which in this case are width=100 and quality=low.
Here's the function I wrote to parse one of these settings strings:
public static Dictionary<string, Dictionary<string, string>> getSettings(string settingsStr)
{
return settingsStr.Split('|').ToDictionary(
template => template.Split('_')[0],
template => template.Split('_')[1].Split(',').ToDictionary(
setting => setting.Split('=')[0],
setting => setting.Split('=')[1]));
}
It just uses a lot of string .Splitting and .ToDictionarying.
Here's the test, showing that it works:
var result = getSettings("full_w=670|small_w=100,q=low|tiny_h=30,c=true");
/*
result = {
[ "full" => [ "w" => "670" ] ]
[ "small" => [ "w" => "100", "q" => "low" ] ]
[ "tiny" => [ "h" => "30", "c" => "true" ] ]
}
*/
To read the values w and q from template small, you can do this:
int width = int.Parse(result["small"]["w"]);
string quality = result["small"]["q"];
Edit: As an added bonus, if you want to convert the Dictionary<string, Dictionary<string, string>> back into a single settings sting, you can use this method:
public static string getSettingsStr(Dictionary<string, Dictionary<string, string>> settings)
{
return string.Join("|",
settings.Select(kvp =>
kvp.Key + "_" + string.Join(",",
kvp.Value.Select(setting =>
setting.Key + "=" + setting.Value))));
}
Use:
string settingsStr = getSettingsStr(result);
// settingsStr = "full_w=670|small_w=100,q=low|tiny_h=30,c=true"
If you want to check that a specific template or setting exists, then use the .ContainsKey() method:
// If I have "Dictionary<string, Dictionary<string, string>> settings;"
int width = -1;
string quality = null;
if (settings.ContainsKey("small"))
{
if (settings["small"].ContainsKey("w"))
width = int.Parse(settings["small"]["w"]);
if (settings["small"].ContainsKey("q"))
quality = settings["small"]["q"];
}
Have you considered using plain old XML Serialization with your own plain old C# objects. Here is an example:
public class Program
{
static void Main(string[] args)
{
var data = new MyConfig[2];
for (int i = 0; i < 2; i++)
{
data[i] = new MyConfig { Name = "Name" + i };
data[i].Properties = new MyConfigAttribute[]
{
new MyConfigAttribute { Name = "Property Name " + i, Value = "Property Value " + i },
new MyConfigAttribute { Name = "2nd Property Name " + i, Value = "2nd Property Value " + i },
};
}
var serializer = new XmlSerializer(typeof(MyConfig[]));
using (StreamWriter tw = File.CreateText(#"c:\temp\myconfig.xml"))
{
serializer.Serialize(tw, data);
}
using (StreamReader tw = File.OpenText(#"c:\temp\myconfig.xml"))
{
var readBack = serializer.Deserialize(tw);
}
Console.ReadLine();
}
[XmlRoot("MY_CONFIG")]
public class MyConfig
{
[XmlElement("NAME")]
public string Name { get; set; }
[XmlArray]
[XmlArrayItem(typeof(MyConfigAttribute))]
public MyConfigAttribute[] Properties { get; set; }
}
[XmlRoot("MY_CONFIG_ATTRIBUTE")]
public class MyConfigAttribute
{
[XmlElement("ATTRIBUTE_NAME")]
public string Name { get; set; }
[XmlElement("ATTRIBUTE_VALUE")]
public string Value { get; set; }
}
}
Basically, you create a class to store your individual attributes (MyConfigAttribute in this case), wrap it in another class to provide your name for a group of related attributes (MyConfig in this case), then use normal XML Serialization to write the settings out to an individual XML file, like this section of the code
var serializer = new XmlSerializer(typeof(MyConfig[]));
using (StreamWriter tw = File.CreateText(#"c:\temp\myconfig.xml"))
{
serializer.Serialize(tw, data);
}
You can read it back to objects again using this section of the code:
using (StreamReader tw = File.OpenText(#"c:\temp\myconfig.xml"))
{
var readBack = serializer.Deserialize(tw);
}
The advantage of this is:
It is simple to understand and use
You can add features to your custom class, e.g. to add values to the array of properties, thereby lending itself to wrapping a custom screen around it.
Look up C# XML Serialization on Google!