0

The 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 title. How can I do this in 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();
        }

    }
   
}
1
  • I doubt about reducing the JSON response itself. I think it needs to be taken care at the service level. Commented Mar 31, 2017 at 16:58

1 Answer 1

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);
 }
Sign up to request clarification or add additional context in comments.

8 Comments

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`
I think you need to import using System.Web.Helpers; then it should work.
json is not the same as Json.
I already fix this but no such a Helper namespace is in System.web
Maybe this link will help you stackoverflow.com/questions/8037895/…
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.