2

Windows Phone 8 の開発は初めてです... Windows Phone 8 で次のデータを解析する方法:

   [
   {
      "detail":{
         "single_selection":[
            {
               "Questions":"If  -2<1\/2x< 4 ,  then",
               "Question Type":"text",
               "Url":"NO URL",
               "options":{
                  "2379":"4 > x < -8",
                  "2380":"4 < x > -8",
                  "2381":"4 < x < -8",
                  "2382":"4 > x > -8"
               },
               "correct Ans":[
                  "2382"
               ],
               "marks":"1",
               "description":"4 > x > -8"
            }
         ]
      }
   }
]

次の方法で解析しようとしています。

 namespace TestSample
{
    public partial class MainPage : PhoneApplicationPage
    {
        private const string Con_String = @"isostore:/DataBase.sdf";
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainPage_Loaded);         


        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            WebClient webClient = new WebClient();
            webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
            webClient.DownloadStringAsync(new Uri("SomeURL"));
        }

        public void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            var rootObject = JsonConvert.DeserializeObject<RootObject1[]>(e.Result);

            foreach (var r in rootObject)
            {
                var s = r.detail.single_selection;
                for (int i = 0; i < s.Count; i++)
                {


                }
            }






        }


        public class RootObject1
        {
            public Detail detail { get; set; }
            [JsonProperty("total questions and marks")]
            public List<TotalQuestionsAndMark> totalquestionsandmarks { get; set; }
        }

        public class TotalQuestionsAndMark
        {
            [JsonProperty("total questions")]
            public int totalquestions { get; set; }
            [JsonProperty("total test marks")]
            public int totaltestmarks { get; set; }
            public string Duration { get; set; }
        }

        public class Detail
        {
            public List<SingleSelection> single_selection { get; set; }            
        }

        public class SingleSelection
        {
            public string Questions { get; set; }
            [JsonProperty("Question Type")]
            public string QuestionType { get; set; }
            public string Url { get; set; }
            public string marks { get; set; }
            public string description { get; set; }
            public Options option { get; set; }
            [JsonProperty("correct Ans")]
            public List<string> correctAns { get; set; }
        }

        public class Options
        {


            public string optionid { get; set; }
            public string option_name { get; set; }


        }
    }
}

一部のデータは解析できますが、オプションを解析する方法がわかりません.完全なコードを解析するのを手伝ってください.完全なデータを解析するのを手伝ってください...事前に感謝します....

4

3 に答える 3

0

サンプルのコンバーター クラスを次に示します。

// Warning: untested code
public class DictionaryConverter: JsonConverter
{
    public override bool CanRead { get { return true; } }
    public override bool CanWrite { get { return false; } }

    public override bool CanConvert( Type objectType )
    {
        return objectType == typeof( Dictionary<string, string> );
    }

    public override object ReadJson( JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer )
    {
        // Load JObject from stream
        JObject jObject = JObject.Load( reader );
        Dictionary<string, string> res = new Dictionary<string, string>( jObject.Count );
        foreach( var kvp in jObject )
            res[ kvp.Key ] = (string)kvp.Value;
        return res;
    }

    public override void WriteJson( JsonWriter writer, object value, JsonSerializer serializer )
    {
        throw new NotSupportedException();
    }
}

options次に、次の方法でフィールド/プロパティを宣言します。

[JsonProperty, JsonConverter( typeof( DictionaryConverter ) )]
public Dictionary<string, string> options;

PSコンバーターを変更してDictionary<int, string>Dictionary<string, string>.

于 2013-07-26T17:13:30.670 に答える
0

コードの問題は、キーと値のペアの辞書をオブジェクト項目の配列に解析しようとしていることです。

解析エンジンは、optionid と option_name の両方がオプション リスト内の各オブジェクト項目の属性であると見なすため、これは不可能です。これらが属性であると仮定すると、属性には定数名が必要であり、json には当てはまりません。

これを解析する唯一の方法は、文字列/文字列のキーと値のペアの辞書を使用することです。コードは次のようにする必要があります。Options クラスを削除する必要があります。

    [JsonProperty(PropertyName = "options")]
    public Dictionary<string, string> Options { get; set; } 
于 2013-07-26T17:13:47.150 に答える