Question Parse JSON string

jaykappy

Member
Joined
Nov 17, 2020
Messages
6
Programming Experience
1-3
I have a JSON string being returned from a function. I want to write each of the values to variables and do something with them.

This is the format my JSON is in....
I am simply trying to write the attributes to variables...

I was trying something like this but not working

C#:
{
   "objectIdFieldName":"OBJECTID",
   "globalIdFieldName":"GlobalID",
   "geometryType":"esriGeometryPolygon",
   "spatialReference":{
      "wkid":102100,
      "latestWkid":3857
   },
   "fields":[
      {
         "name":"OBJECTID",
         "alias":"OBJECTID",
         "type":"esriFieldTypeOID"
      },
      {
         "name":"ADDRESSgeocode",
         "alias":"ADDRESSgeocode",
         "type":"esriFieldTypeString",
         "length":255
      },
      {
         "name":"DISTANCEparameter",
         "alias":"DISTANCEparameter",
         "type":"esriFieldTypeString",
         "length":5
      },
      {
         "name":"UNIQUEIDparameter",
         "alias":"UNIQUEIDparameter",
         "type":"esriFieldTypeString",
         "length":80
      },
      {
         "name":"COUNTYList",
         "alias":"COUNTYList",
         "type":"esriFieldTypeString",
         "length":550
      },
      {
         "name":"NWTLID",
         "alias":"NWTLID",
         "type":"esriFieldTypeGUID",
         "length":38
      },
      {
         "name":"GlobalID",
         "alias":"GlobalID",
         "type":"esriFieldTypeGlobalID",
         "length":38
      }
   ],
   "features":[
      {
         "attributes":{
            "OBJECTID":42229,
            "ADDRESSgeocode":"816 W Main St, Danville, Virginia(VA), 24541",
            "DISTANCEparameter":"12",
            "UNIQUEIDparameter":"{BA814524-95FC-443C-9813-B4FE42099999}",
            "COUNTYList":"83,143,590",
            "NWTLID":"{BA814524-95FC-443C-9813-B4FE42099999}",
            "GlobalID":"{48E1D562-A06C-48B2-8588-91CCE9D3EF1F}"
         }
      }
   ]
}
 
Last edited by a moderator:
I try this below and get cant convert object to string

1605642711572.png
 
I GOT IT...


JObject rss = JObject.Parse(ResponsePolygonCheckExists);
string itemTitle = (string)rss["features"][0]["attributes"]["ADDRESSgeocode"];
txt_CHECKifPOlygonExist.Text = itemTitle;
 
Sure that may work, but how well does that solution scale? Cut and paste to make it scale up?
What happens when you start cutting and pasting and someone makes a typo on one of the field names? You won't find out until runtime.
 
You can also use the Value method to convert, this also allows selecting a key from a common token, example:
C#:
var token = rss.SelectToken("features[0].attributes"); //same as rss["features"][0]["attributes"]
var address = token.Value<string>("ADDRESSgeocode");
var distance= token.Value<int>("DISTANCEparameter");
 
Sure that may work, but how well does that solution scale? Cut and paste to make it scale up?
What happens when you start cutting and pasting and someone makes a typo on one of the field names? You won't find out until runtime.
Any suggestions Skydiver?
 
Back
Top Bottom