2017-03-31 9 views
0

C#を使用してJSON URLを解析するいくつかの問題に直面しています。ご存知のように、JSONデータは名前と値のペアとして記述されています。今URLのJSONに私は、これらのデータがあります。JSON名のペアをC#で解析する

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

をし、私はこのコード

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) 
    { 

    } 
} 

を使用してJSONデータの名前の一部を印刷したいが、これは、コンソール上の任意のものを印刷していません!私が間違っていることを教えていただけますか?

+1

可能性のある重複した[URLからJSON文字列を取得する方法?](http://stackoverflow.com/questions/5566942/how-to-get-a-json-string-from-url) – scrappedcola

+0

あなたはどんなエラーを出していますか?あなたは例外を食べています。 –

+0

私は何もエラーが発生していません!ただコンソールを使用します – Behseini

答えて

2

使用Newtonsoft JSON

JObject jsonObject = JObject.Parse(json); 
foreach(var jsonItem in jsonObject) 
{ 
    Console.WriteLine(jsonItem.Key); 
} 
Console.ReadKey(); 
0

結果

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; } 
} 

結果をデシリアライズするためにJavaScriptSerializerを使用を保持するオブジェクトを作成します。

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

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

あなたはデバッグの下でこれを実行している場合は、こちらをご覧ください:Attempt by method 'System.Web.Helpers.Json..cctor()' to access method 'System.Web.Helpers.Json.CreateSerializer()' failed私はそれが結果と実行のVisual Studioホスティングプロセスを有効にオフに一度。しかし、私はあなたが欲しいと思うものを手に入れる(キー/値ペアのそれぞれのリストは、私はforeachのに切り替え、それはうまくそれをプリントアウト:の

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);  
    } 
関連する問題