gpt4 book ai didi

c# - 在 Windows Phone 8 中解析复杂的 JSON 对象(字典键/值对)

转载 作者:太空宇宙 更新时间:2023-11-03 16:10:04 25 4
gpt4 key购买 nike

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


}
}
}

我能够解析一些数据,但我不知道如何解析选项。请帮我解析完整的代码。请帮我解析完整的数据……提前谢谢……

最佳答案

您的代码存在的问题是您试图将键值对字典解析为对象项数组。

这是不可能的,因为解析引擎会认为 optionid 和 option_name 都是选项列表中每个对象项的属性。如果我们假设这些是属性,则属性必须有一个常量名称,而在您的 json 中不是这种情况。

唯一可能的解析方法是使用字符串/字符串的键值对字典。你的代码必须是这样的,你应该删除选项类。

    [JsonProperty(PropertyName = "options")]
public Dictionary<string, string> Options { get; set; }

关于c# - 在 Windows Phone 8 中解析复杂的 JSON 对象(字典键/值对),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17832704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com