2011-11-20 14 views
5

Windows PhoneでこのJSONを読もうとしています。私が気にすべての人々のセクションのエントリであるWP7でJSONを逆シリアル化する

{"lastUpdated":"16:12","filterOut":[],"people": 
[{"ID":"x","Name":"x","Age":"x"},{"ID":"x","Name":"x","Age":"x"},{"ID":"x","Name":"x","Age":"x"}], 
"serviceDisruptions": 
    { 
    "infoMessages": 
    ["blah blah text"], 
    "importantMessages": 
    [], 
    "criticalMessages": 
    [] 
    } 
} 

:私はDataContractJsonSerializerとJson.NETで遊んでますが、特に各「エントリー」を読んで、あまり運があってきました。基本的には、エントリ(ID、名前、年齢の値を含む)を読み込んで繰り返し、コレクションまたはクラスに追加する必要があります。 (後でリストボックスを作成しています)

+0

あなたの例有効なjsonではありません、本当の事を投稿してください:-) – abcde123483

+0

私はちょうどテキストを変更し、それを別々の行に整形しました。それはなぜ有効ではないと言いますか? –

+0

明瞭にするために編集されました。 –

答えて

6

次のコードを使用してJSON文字列を逆シリアル化できました。これは.NET 4コンソールアプリケーションでテストされ、うまくいけばWP 7でもうまくいきます。

次のデータクラスを使用して
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(PersonCollection)); 

string json = "{\"lastUpdated\":\"16:12\",\"filterOut\":[],\"people\": [{\"ID\":\"a\",\"Name\":\"b\",\"Age\":\"c\"},{\"ID\":\"d\",\"Name\":\"e\",\"Age\":\"f\"},{\"ID\":\"x\",\"Name\":\"y\",\"Age\":\"z\"}], \"serviceDisruptions\": { \"infoMessages\": [\"blah blah text\"], \"importantMessages\": [], \"criticalMessages\": [] } }"; 

using (var stream = new MemoryStream(Encoding.Unicode.GetBytes(json))) 
{ 
    var people = (PersonCollection)serializer.ReadObject(stream); 

    foreach(var person in people.People) 
    { 
     Console.WriteLine("ID: {0}, Name: {1}, Age: {2}", person.ID, person.Name, person.Age); 
    } 
} 

[DataContract] 
public class PersonCollection 
{ 
    [DataMember(Name = "people")] 
    public IEnumerable<Person> People { get; set; } 
} 

[DataContract] 
public class Person 
{ 
    [DataMember] 
    public string ID { get; set; } 

    [DataMember] 
    public string Name { get; set; } 

    [DataMember] 
    public string Age { get; set; } 
} 
+1

あなたはそれに私を打ち負かす、はいあなたのJSONは有効です:http://jsonlint.com/ – invalidusername

+0

これは完璧なおかげで、私のコレクションを埋めるためにうまく修正しました。私のためのトリッキーなビットは、それを列挙する方法でしたが、PersonCollectionはそれをうまく分類するようです。 –

1

以下のソリューションがJson.NETを使用しています。 JSON文字列をまずXMLに逆シリアル化し、LINQ to XMLを使用してすべての人口ノードを反復し、Personクラスのインスタンスに変換します。

private class Person 
{ 
    public string ID { get; set; } 
    public string Name { get; set; } 
    public string Age { get; set; } 
} 

// deserializes your JSON and creates a list of Person objects from it 
private void button1_Click(object sender, RoutedEventArgs e) 
{ 
    // your JSON 
    string json = 
     "{\"lastUpdated\":\"16:12\",\"filterOut\":[],\"people\": " + 
     "[{\"ID\":\"x\",\"Name\":\"x\",\"Age\":\"x\"},{\"ID\":\"x\",\"Name\":\"x\",\"Age\":\"x\"},{\"ID\":\"x\",\"Name\":\"x\",\"Age\":\"x\"}]," + 
     "\"serviceDisruptions\":" + 
     "{" + 
     "\"infoMessages\":" + 
     "[\"blah blah text\"]," + 
     "\"importantMessages\":" + 
     "[]," + 
     "\"criticalMessages\":" + 
     "[]" + 
     "}" + 
     "}"; 

    // deserialize from JSON to XML 
    XDocument doc = JsonConvert.DeserializeXNode(json, "root"); 

    // iterate all people nodes and create Person objects 
    IEnumerable<Person> people = from person in doc.Element("root").Elements("people") 
           select new Person() 
           { 
            ID = person.Element("ID").Value, 
            Name = person.Element("Name").Value, 
            Age = person.Element("Age").Value 
           }; 

    // this is just demonstrating that it worked 
    foreach (Person person in people) 
     Debug.WriteLine(person.Name); 
} 

輸入を忘れないでください:

using Newtonsoft.Json; 
using System.Xml.Linq; 
using System.Diagnostics; 

そして、これは非直列化されたJSONは(そこに好奇心の人々のための)XML文書としてどのように見えるかです:

<root> 
    <lastUpdated>16:12</lastUpdated> 
    <people> 
    <ID>x</ID> 
    <Name>x</Name> 
    <Age>x</Age> 
    </people> 
    <people> 
    <ID>x</ID> 
    <Name>x</Name> 
    <Age>x</Age> 
    </people> 
    <people> 
    <ID>x</ID> 
    <Name>x</Name> 
    <Age>x</Age> 
    </people> 
    <serviceDisruptions> 
    <infoMessages>blah blah text</infoMessages> 
    </serviceDisruptions> 
</root> 
+0

これを試したことはありませんが、あなたの答えに感謝します。 –

関連する問題