Geographic Information Systems Asked on January 22, 2021
I want to get ArcGIS JSON response (like here) from server and save it to my database. I am using .Net core and entity framework net topologysuite for CRUD operations database. But I coud not convert the Esri JSON object to .Net object. Is there any tool for this?
You can use a dynamic object in C#. I'm taking a guess here that you are using C# as there is no code posted.
I'm doubting you have an object that exactly matches the ESRI response object, and I doubt you want to take the time to create one. This is what you can use a dynamic object for. In .net core you can use the System.Text.Json namespace to Deserialize the response into a dynamic object. This should answer your question...
However, I'm not sure you need to convert this to an object to save to the database. It's JSON, which is just a string. Why can't you just save the JSON string into the database? If you need to use it later you pull that data and Deserialize it into an object.
class Program {
static async System.Threading.Tasks.Task Main(string[] args) {
string baseUrl = "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Water_Network/FeatureServer/8/query?where=1%3D1&objectIds=&time=&geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects&distance=&units=esriSRUnit_Foot&relationParam=&outFields=*&returnGeometry=true&maxAllowableOffset=&geometryPrecision=&outSR=&having=&gdbVersion=&historicMoment=&returnDistinctValues=false&returnIdsOnly=false&returnCountOnly=false&returnExtentOnly=false&orderByFields=&groupByFieldsForStatistics=&outStatistics=&returnZ=false&returnM=false&multipatchOption=xyFootprint&resultOffset=&resultRecordCount=&returnTrueCurves=false&returnExceededLimitFeatures=false&quantizationParameters=&returnCentroid=false&sqlFormat=none&resultType=&featureEncoding=esriDefault&f=pjson";
try {
using (HttpClient client = new HttpClient()) {
using (HttpResponseMessage res = await client.GetAsync(baseUrl)) {
using (HttpContent content = res.Content) {
var data = await content.ReadAsStringAsync();
if (data != null) {
// This line will allow you to get a .net object from the JSON
var result = JsonSerializer.Deserialize<dynamic>(data);
// Here it's being serialized again to write to the console
Console.WriteLine("ESRI Data ------------ {0}", JsonSerializer.Serialize(result));
}
}
}
}
} catch (Exception exception) {
Console.WriteLine("Exception:");
Console.WriteLine(exception);
}
}
}
Hope that helps.
Correct answer by GetFuzzy on January 22, 2021
What I often do in these sort of cases is create a class based on a (representative) example of the JSON response (i.e. one that has most or all properties set to a value, rather than null):
You may need to hand-edit some types, because VS will can only do a best guess, based on whatever is in your copied JSON sample. For instance, if a value is 1
, the type might be set to byte
or short
, rather then int
. If you then try to deserialize a value outside the short
range, your application will throw an exception.
In your specific example, since the geometry is a point, the generated Geometry class will only have x
and y
fields, and therefore will not match a line or polygon.
Deserialization is done like this:
return JsonSerializer.Deserialize<myArcGISClass>(json);
Answered by Berend on January 22, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP