2017-08-17 7 views
1

いくつかのJSONコードから配列を取得しようとしています。
私はここからです:JSON code sourceオブジェクトを含むjsonリストを読み取るC#

私はこれを持っていますが、私はどのように出力を使用可能にするか分かりません。

 //Some other code above this line 
     var jsonout = new JavaScriptSerializer().Deserialize<List<Rootobject>>(json); 

    } 
} 

//JSON structure 
public class Rootobject 
{ 
    public Class1[] Property1 { get; set; } 
} 

public class Class1 
{ 
    public string group { get; set; } 
    public string tracker { get; set; } 
    public string measureTime { get; set; } 
    public int minAgo { get; set; } 
    public float lat { get; set; } 
    public float lon { get; set; } 
    public History[] history { get; set; } 
} 

public class History 
{ 
    public float lat { get; set; } 
    public float lon { get; set; } 
    public int minAgo { get; set; } 
} 

は私がlatlonmeasureTime、など。出力からを取得する方法には考えています。あなたはそれをやる方法について素敵なやり方をしていますか? (私はC#でJSONを使用することで非常に新しいです)。

+0

Json.NETではなくJavaScriptSerializerを使用する必要がありますか?あなたの現在のコードは実際に何をしていますか? (リストではなく 'List 'を逆シリアル化する必要があります) –

+0

データモデルが間違っています - 余分なレベルの 'Class1'は不要です。 JSONをhttp://json2csharp.com/に投稿すると、 'RootObject'が' Class1'のプロパティを持つ修正済みのデータモデルを得ることができます。 – dbc

答えて

1

データモデルが間違っています - 余分なレベルClass1は不要です。 http://json2csharp.com/にあなたのJSONを投稿し、あなたはRootObjectClass1からの性質を有する補正されたデータモデル、取得することができます:

をそして実行します。

var jsonout = new JavaScriptSerializer().Deserialize<List<RootObject>>(json); 
foreach (var root in jsonout) 
{ 
    Console.WriteLine(root.measureTime); // For instance. 
    Console.WriteLine(root.lat); // For instance. 
    Console.WriteLine(root.lon); // For instance. 
} 
0

間違ったタイプを逆シリアル化しています。 Rootobjectのコレクションがなく、Class1のコレクションを含む単一のRootobjectがあります。

var jsonout = new JavaScriptSerializer().Deserialize<Rootobject>(json); 

この時点でオブジェクト表記を使用してください。

foreach(var thing in jsonout.Property1) 
{ 
    thing.lat; 
    thing.lon; 
} 
0

あなたはこの

ようなあなたの一般的なクラスを宣言することができます
public class Class1 
{ 
    public string group { get; set; } 
    public string tracker { get; set; } 
    public string measureTime { get; set; } 
    public int minAgo { get; set; } 
    public float lat { get; set; } 
    public float lon { get; set; } 
    public List<History> history { get; set; } 

    public List<History> GetListHistories(){ 
     return history; 
    } 
} 

public class History 
{ 
    public float lat { get; set; } 
    public float lon { get; set; } 
    public int minAgo { get; set; } 
} 

このような実装と

var jsonout = new JavaScriptSerializer().Deserialize<List<Class1>>(json); 
foreach(var item in jsonout) 
{ 
    console.Write(item.gruop); 
    console.Write(item.tracker); 
    // more properties 
    // and: 
    List<History> list = item.GetListHistories(); 
    foreach(var l in list) 
    { 
    console.Write(l.lat); 
    console.Write(l.lon); 
    } 
} 
関連する問題