0

Following code returns entire JSON objects but I need to Filter to some specific Object, for example I need to get only the "layers" or tiltle can you please let me know how to do this is C#?

Do I have to create an HttpWebRequestobject for this? if so where should I pass the requested data?

using (WebClient wc = new WebClient())
   {
    var json = wc.DownloadString("https://sampleserver6.arcgisonline.com/arcgis/rest/services/Water_Network/FeatureServer?f=pjson");
    Console.WriteLine(json);
   }

I already tried this but it is also returning everything

class Program
{
    private const string URL = "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Water_Network/FeatureServer?f=pjson";
    private const string DATA = @"{{""layers"":""Layers""}}";
    static void Main(string[] args)
    {

       CreateObject();

    }
    private static void CreateObject()
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
        request.Method = "POST";
        request.ContentType = "application/json";
        request.ContentLength = DATA.Length;
        using (Stream webStream = request.GetRequestStream())
        using (StreamWriter requestWriter = new StreamWriter(webStream, System.Text.Encoding.ASCII))
        {
            requestWriter.Write(DATA);
        }

        try
        {
            WebResponse webResponse = request.GetResponse();
            using (Stream webStream = webResponse.GetResponseStream())
            {
                if (webStream != null)
                {
                    using (StreamReader responseReader = new StreamReader(webStream))
                    {
                        string response = responseReader.ReadToEnd();
                        Console.WriteLine(response);
                        Console.ReadLine();
                    }
                }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("-----------------");
            Console.WriteLine(e.Message);
            Console.ReadLine();
        }

    }

}
Behseini
  • 6,066
  • 23
  • 78
  • 125
  • I doubt about reducing the JSON response itself. I think it needs to be taken care at the service level. – tRuEsAtM Mar 31 '17 at 16:58

1 Answers1

2

If you need the info related to the layers array object then you can use following code

 using (var wc = new WebClient())
 {
     string json = wc.DownloadString("https://sampleserver6.arcgisonline.com/arcgis/rest/services/Water_Network/FeatureServer?f=pjson");
     dynamic data = Json.Decode(json);
     Console.WriteLine(data.layers[0].id);
     Console.WriteLine(data.layers[0].name);
     Console.WriteLine(data.documentInfo.Title);
 }
tRuEsAtM
  • 3,517
  • 6
  • 43
  • 83
  • Thanks for reply Sammer but I am getting this error `'string' does not contain a defination fo 'Decode` and no extension method 'Decode" accepting a first argument type of string` – Behseini Mar 31 '17 at 17:04
  • I think you need to import `using System.Web.Helpers;` then it should work. – tRuEsAtM Mar 31 '17 at 17:06
  • json is not the same as Json. – Alex Paven Mar 31 '17 at 17:07
  • I already fix this but no such a `Helper` namespace is in `System.web` – Behseini Mar 31 '17 at 17:08
  • Maybe this link will help you http://stackoverflow.com/questions/8037895/where-can-i-find-system-web-helpers-system-web-webpages-and-system-web-razor – tRuEsAtM Mar 31 '17 at 17:10
  • Ok I got the assembly for web Helper but now I am getting Field Access Exception Error on `Additional information: Attempt by method 'System.Web.Helpers.Json.Decode(System.String)' to access field 'System.Web.Helpers.Json._serializer' failed.` when I run the application – Behseini Mar 31 '17 at 17:16
  • Look into this http://stackoverflow.com/questions/16416557/attempt-by-method-system-web-helpers-json-decodesystem-string-to-access-fiel – tRuEsAtM Mar 31 '17 at 17:19
  • It is working fine for getting `Console.WriteLine(data.documentInfo.Title);` but not working for getting the layes! – Behseini Mar 31 '17 at 17:28