0

I am using C# to parse JSON URL. As you know JSON data is written as name/value pairs. Now in URL JSON I have these data:

{  
   "currentVersion":10.41,
   "serviceDescription":"There are some text here",
   "hasVersionedData":true,
   "supportsDisconnectedEditing":false,
   "syncEnabled":false,
   "supportedQueryFormats":"JSON",
   "maxRecordCount":1000
 }

and I want to only print out the name part of the JSON data using this code:

using (var wc = new WebClient())
{
    string json = wc.DownloadString("http://xxxxxxxxx?f=pjson");
    try
    {
        dynamic data = Json.Decode(json);
        for (int i = 0; i <= data.Length - 1; i++)
        {
            Console.WriteLine(data[0]);
        }
        
    }
    catch (Exception e)
    {

    }
}

but this is not printing anything on the console. What am I doing wrong?

6
  • 1
    Possible duplicate of How to get a json string from url? Commented Mar 31, 2017 at 18:14
  • What error are you getting? You are eating up the exception. Try write to console and see the error Commented Mar 31, 2017 at 18:14
  • I am not getting any error! just emply console Commented Mar 31, 2017 at 18:15
  • how do u know u are not getting error when u are ignoring exceptions? Commented Mar 31, 2017 at 18:16
  • I already write down the console on catch so no error Commented Mar 31, 2017 at 18:17

3 Answers 3

2

Use Newtonsoft JSON:

JObject jsonObject = JObject.Parse(json);
foreach(var jsonItem in jsonObject)
{
    Console.WriteLine(jsonItem.Key);
}
Console.ReadKey();
Sign up to request clarification or add additional context in comments.

Comments

0

Create an object to hold the results

public class RootObject
{
    public double currentVersion { get; set; }
    public string serviceDescription { get; set; }
    public bool hasVersionedData { get; set; }
    public bool supportsDisconnectedEditing { get; set; }
    public bool syncEnabled { get; set; }
    public string supportedQueryFormats { get; set; }
    public int maxRecordCount { get; set; }
}

Use a JavaScriptSerializer to deserialize the result.

var serializer = new JavaScriptSerializer();
var rootObject= serializer.Deserialize<RootObject>(json);

Console.WriteLine(rootObject.currentVersion);
Console.WriteLine(rootObject.serviceDescription);
etc.

Comments

0

If you are running this under Debug, see here: Attempt by method 'System.Web.Helpers.Json..cctor()' to access method 'System.Web.Helpers.Json.CreateSerializer()' failed once I unchecked Enable the Visual Studio hosting process it run with results. However, to get what I think you want (a listing of each of the key/value pairs I switched to a foreach and it printed it out nicely:

try   

    {    
        var data = Json.Decode(jsonData);    
        //for (var i = 0; i <= data.Length - 1; i++)    
        foreach (var j in data)    
        {    
            Console.WriteLine(j);    
        }    
    }    
    catch (Exception e)    
    {    
        Console.WriteLine(e);    
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.