4

I need to get the JSON from a feature using ArcObjects. According to I tried to following code, but it does not work:

var jsonWriter = new JSONWriterClass();
jsonWriter.WriteToString();

var jsonSerializer = (IExternalSerializerGdb2) new JSONSerializerGdbClass();
var options = new GeometryResultOptionsClass();
var fieldIndexes = Enumerable.Range(0, feature.Fields.FieldCount).ToArray();
serializer.WriteRow("", feature, feature.Fields, fieldIndexes, options);
var json = Encoding.UTF8.GetString(jsonWriter.GetStringBuffer());

I allways get the error in the line serializer.WriteRow("", feature, feature.Fields, fieldIndexes, options):

Value does not fall within the expected range. (Source: ESRI.ArcGIS.Geodatabase)

Does anybody know how to call the WriteRow(...) method correctly

  • Possible duplicate: gis.stackexchange.com/questions/202865/… – Berend Oct 31 '18 at 10:44
  • It's not asked for GeoJson but for ESRI json and another method is used. Just for a single feature. – scher Oct 31 '18 at 10:47
  • Did you try putting your feature into a RecordSet and call WriteRecordSet instead? – Kirk Kuykendall Oct 31 '18 at 13:41
  • Did you try debugging and getting the values of the variables that you are using? It could be as simple as index out of range or something to that effect. – enolan Oct 31 '18 at 18:32
2

You have forgotten to initialize the serializer. This code might work, at least it did for me:

var jsonWriter = new JSONWriterClass();
jsonWriter.WriteToString();

var jsonSerializer = new JSONSerializerGdbClass();
jsonSerializer.InitSerializer(jsonWriter, null); // <--
var options = new GeometryResultOptionsClass();
var fieldIndexes = Enumerable.Range(0, feature.Fields.FieldCount).ToArray();
((IExternalSerializerGdb2)serializer).WriteRow("", feature, feature.Fields, fieldIndexes, options);
var json = Encoding.UTF8.GetString(jsonWriter.GetStringBuffer());

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.