2017-06-21 13 views
0

これは私の最初の投稿です:) 私はjsonの文字列を逆直列化に問題があります。c#。このJsonを逆シリアル化する方法

{ 
"packs": { 
    "category1": { 
     "Element1": { 
      "url": "Url1", 
      "name": "File 1" 
     }, 
     "Element2": { 
      "url": "Url2", 
      "name": "File 2" 
     }, 
     "Element3": { 
      "url": "Url3", 
      "name": "File 3" 
     }, 
     "Element4": { 
      "url": "Url4", 
      "name": "File 4" 
     }, 
     "Element5": { 
      "url": "Url5", 
      "name": "File 5" 
     }, 
     "Element6": { 
      "url": "Url6", 
      "name": "File 6" 
     }, 
     "Element7": { 
      "url": "Url7", 
      "name": "File 7" 
     }, 
     "Element8": { 
      "url": "Url8", 
      "name": "File 8" 
     }, 
     "Element9": { 
      "url": "Url9", 
      "name": "File 9" 
     }, 
     "Element10": { 
      "url": "Url10", 
      "name": "File 10" 
     } 
    }, 
    "category2": { 
     "short": { 
      "url": "Url1", 
      "name": "Short " 
     }, 
     "medium": { 
      "url": "Url2", 
      "name": "Medium " 
     }, 
     "long": { 
      "url": "Url3", 
      "name": "Long " 
     } 
    } 
} 
} 

これでコードのデシリアライズ: これは一例です

var json = client.GetStringAsync(string.Format(Url)); 

var jsonDeserialize = JsonConvert.DeserializeObject<Models.PacksModel>(json.Result); 

そして、これはモデルです:

public class PacksModel 
{ 
    public Cathegory packs { get; set; } 
} 


public class Cathegory 
{ 
    public JContainer category1 { get; set; } 
    public JContainer category2 { get; set; } 
} 

私は要素からデータを取得するための任意のアイデアを持っていません。どんな甘いもの?回答のために Thx!

+0

json文字列が間違っています。 http://json2csharp.com/を使用し、間違ったモデルを使用していることを自分自身で確認してください – Rahul

+0

いいえ、JSON文字列は有効です。マーキングコードの外側}を忘れないでください。 –

答えて

0

これはあなたのJSONをデシリアライズするためにオブジェクトを試してみてください:

namespace ConsoleApp3.Domain 
{ 
    public class Element1 
    { 
     public string url { get; set; } 
     public string name { get; set; } 
    } 

    public class Element2 
    { 
     public string url { get; set; } 
     public string name { get; set; } 
    } 

    public class Element3 
    { 
     public string url { get; set; } 
     public string name { get; set; } 
    } 

    public class Element4 
    { 
     public string url { get; set; } 
     public string name { get; set; } 
    } 

    public class Element5 
    { 
     public string url { get; set; } 
     public string name { get; set; } 
    } 

    public class Element6 
    { 
     public string url { get; set; } 
     public string name { get; set; } 
    } 

    public class Element7 
    { 
     public string url { get; set; } 
     public string name { get; set; } 
    } 

    public class Element8 
    { 
     public string url { get; set; } 
     public string name { get; set; } 
    } 

    public class Element9 
    { 
     public string url { get; set; } 
     public string name { get; set; } 
    } 

    public class Element10 
    { 
     public string url { get; set; } 
     public string name { get; set; } 
    } 

    public class Category1 
    { 
     public Element1 Element1 { get; set; } 
     public Element2 Element2 { get; set; } 
     public Element3 Element3 { get; set; } 
     public Element4 Element4 { get; set; } 
     public Element5 Element5 { get; set; } 
     public Element6 Element6 { get; set; } 
     public Element7 Element7 { get; set; } 
     public Element8 Element8 { get; set; } 
     public Element9 Element9 { get; set; } 
     public Element10 Element10 { get; set; } 
    } 

    public class Short 
    { 
     public string url { get; set; } 
     public string name { get; set; } 
    } 

    public class Medium 
    { 
     public string url { get; set; } 
     public string name { get; set; } 
    } 

    public class Long 
    { 
     public string url { get; set; } 
     public string name { get; set; } 
    } 

    public class Category2 
    { 
     public Short @short { get; set; } 
     public Medium medium { get; set; } 
     public Long @long { get; set; } 
    } 

    public class Packs 
    { 
     public Category1 category1 { get; set; } 
     public Category2 category2 { get; set; } 
    } 

    public class RootObject 
    { 
     public Packs packs { get; set; } 
    } 
} 

そして、それをデシリアライズする:

namespace ConsoleApp3 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var jsonfile = File.ReadAllText("jsonfile.json"); 
      var deserializedFile = JsonConvert.DeserializeObject<Domain.RootObject>(jsonfile); 
      // Do something with your object 
     } 
    } 
} 
0

あなたのJSON文字列を使用すると、あなたのモデルは次のようになります。

public class Element1 
{ 
    public string url { get; set; } 
    public string name { get; set; } 
} 

public class Element2 
{ 
    public string url { get; set; } 
    public string name { get; set; } 
} 

public class Element3 
{ 
    public string url { get; set; } 
    public string name { get; set; } 
} 

public class Element4 
{ 
    public string url { get; set; } 
    public string name { get; set; } 
} 

public class Element5 
{ 
    public string url { get; set; } 
    public string name { get; set; } 
} 

public class Element6 
{ 
    public string url { get; set; } 
    public string name { get; set; } 
} 

public class Element7 
{ 
    public string url { get; set; } 
    public string name { get; set; } 
} 

public class Element8 
{ 
    public string url { get; set; } 
    public string name { get; set; } 
} 

public class Element9 
{ 
    public string url { get; set; } 
    public string name { get; set; } 
} 

public class Element10 
{ 
    public string url { get; set; } 
    public string name { get; set; } 
} 

public class Category1 
{ 
    public Element1 Element1 { get; set; } 
    public Element2 Element2 { get; set; } 
    public Element3 Element3 { get; set; } 
    public Element4 Element4 { get; set; } 
    public Element5 Element5 { get; set; } 
    public Element6 Element6 { get; set; } 
    public Element7 Element7 { get; set; } 
    public Element8 Element8 { get; set; } 
    public Element9 Element9 { get; set; } 
    public Element10 Element10 { get; set; } 
} 

public class Short 
{ 
    public string url { get; set; } 
    public string name { get; set; } 
} 

public class Medium 
{ 
    public string url { get; set; } 
    public string name { get; set; } 
} 

public class Long 
{ 
    public string url { get; set; } 
    public string name { get; set; } 
} 

public class Category2 
{ 
    public Short @short { get; set; } 
    public Medium medium { get; set; } 
    public Long @long { get; set; } 
} 

public class Packs 
{ 
    public Category1 category1 { get; set; } 
    public Category2 category2 { get; set; } 
} 

public class PacksModel 
{ 
    public Packs packs { get; set; } 
} 

そして:

var json = client.GetStringAsync(string.Format(Url)); 
var jsonDeserialize = JsonConvert.DeserializeObject<Models.PacksModel>(json.Result); 

注意:JSON文字列構造を変更することができる場合は、要素の配列を使用する必要があります。例えば

、あなたはこれにJSON文字列の構造を変更した場合:その後、

public class Element 
{ 
    public string url { get; set; } 
    public string name { get; set; } 
} 

public class Category1 
{ 
    public List<Element> Elements { get; set; } 
} 

public class Short 
{ 
    public string url { get; set; } 
    public string name { get; set; } 
} 

public class Medium 
{ 
    public string url { get; set; } 
    public string name { get; set; } 
} 

public class Long 
{ 
    public string url { get; set; } 
    public string name { get; set; } 
} 

public class Category2 
{ 
    public Short @short { get; set; } 
    public Medium medium { get; set; } 
    public Long @long { get; set; } 
} 

public class Packs 
{ 
    public Category1 category1 { get; set; } 
    public Category2 category2 { get; set; } 
} 

public class PacksModel 
{ 
    public Packs packs { get; set; } 
} 

そして:

var json = client.GetStringAsync(string.Format(Url)); 
var jsonDeserialize = JsonConvert.DeserializeObject<Models.PacksModel>(json.Result); 

労力で

{ 
    "packs": { 
     "category1": { 

      "Elements": [{ 
        "url": "Url1", 
        "name": "File 1" 
       }, 
       { 
        "url": "Url2", 
        "name": "File 2" 
       }, 
       { 
        "url": "Url3", 
        "name": "File 3" 
       }, 
       { 
        "url": "Url4", 
        "name": "File 4" 
       }, 
       { 
        "url": "Url5", 
        "name": "File 5" 
       }, 
       { 
        "url": "Url6", 
        "name": "File 6" 
       }, 
       { 
        "url": "Url7", 
        "name": "File 7" 
       }, 
       { 
        "url": "Url8", 
        "name": "File 8" 
       }, 
       { 
        "url": "Url9", 
        "name": "File 9" 
       }, 
       { 
        "url": "Url10", 
        "name": "File 10" 
       } 
      ] 
     }, 
     "category2": { 
      "short": { 
       "url": "Url1", 
       "name": "Short " 
      }, 
      "medium": { 
       "url": "Url2", 
       "name": "Medium " 
      }, 
      "long": { 
       "url": "Url3", 
       "name": "Long " 
      } 
     } 
    } 
} 

あなたはこのモデルを使用することができますこのJSON文字列構造は、柔軟な要素数を使用できるということです!

1

カテゴリとエレメント名が変化する傾向があるので、あなたが辞書としてそれらを定義したほうが良いことがあります。

public class PacksModel 
{ 
    public Dictionary<string, Dictionary<string, Item>> packs { get; set; } 
} 

public class Item 
{ 
    public string url { get; set; } 
    public string name { get; set; } 
} 

// ...................................... 

var obj = JsonConvert.DeserializeObject<PacksModel>(json); 
Console.WriteLine(obj.packs["category2"]["medium"].url); 

デモ:答えをhttps://dotnetfiddle.net/h4BYOE

0

みんなありがとう。 私はXamarin.Formsでプログラミングを学んでいます。それは私のjsonではありません。

HttpClient client = new HttpClient(); 
var json = client.GetStringAsync(string.Format(Url)); 
dynamic deserializeJson = JsonConvert.DeserializeObject<dynamic>(json.Result); 



private void writeDataOnModelMusicUrl() 
    { 
     foreach (var part in deserializeJson["packs"]["category1"]) 
     { 

      foreach (var elements in part) 
      { 
       var url = elements["url"]; 
       var name = elements["name"]; 
       //I'm doing something 
      } 
     } 

     foreach (var part in deserializeJson["packs"]["category2"]) 
     { 

      foreach (var elements in part) 
      { 
       var url = elements["url"]; 
       var name = elements["name"]; 
       //I'm doing something 
      } 
     } 
    } 

私はそれが十分だと思います:)

対象を閉じることができます。

関連する問題