我有一个Json数据,请建议我如何阅读这种类型的Json。
{"Questions": [
{
"Question": "Who was the Chola King who brought Ganga from North to South?",
"CorrectAnswer": 1,
"Answers": [
{
"Answer": "Raja Raja Chola"
},
{
"Answer": "Rajendra Chola"
},
{
"Answer": "Parantaka"
},
{
"Answer": "Mahendra"
}
]
},
{
"Question": "The writ of 'Habeas Corpus' is issued in the event of:",
"CorrectAnswer": 2 ,
"Answers": [
{
"Answer": "Loss of Property"
},
{
"Answer": "Refund of Excess Taxes"
},
{
"Answer": "Wrongful Police Detention"
},
{
"Answer": "Violation of the Freedom of Speech"
}
]}
]}
答案 0 :(得分:1)
Newtonsoft.Json是我最喜欢的操纵JSON的库。
你的代码应该是这样的:
<强> EDITED 强>
public class AnswerObj{
public string Answer{get;set;}
}
public class QuestionObj{
public string Question {get;set;}
public int CorrectAnswer {get;set;}
public List<AnswerObj> Answers {get;set;}
}
public class QuestionsRepository
{
public List<QuestionObj> Questions {get;set;}
}
//Here is the code for reading your JSON
string json = "Your_JSON_COMES_HERE as a string"
QuestionsRepository questions = JsonConvert.DeserializeObject<QuestionsRepository>(json);
答案 1 :(得分:1)
您可以使用内置的.NET,DataContractJsonSerializer
类,它可用于序列化和反序列化json字符串。 (MSDN Link)
以下是MSDN中的完整教程:(How to: Serialize and Deserialize JSON data)
答案 2 :(得分:0)
我更喜欢至少使用框架。看看我的代码。
对于给定的对象结构:
public class QuestionsRepository
{
public List<QuestionObj> Questions;
}
public class QuestionObj
{
public string Question;
public UInt16 CorrectAnswer;
public AnswerObj[] Answers;
}
public class AnswerObj
{
public string Answer;
}
声明一些简单的最简单的包装器:
public static class JsonUtils
{
class JsonSerializer<T>
{
static DataContractJsonSerializer xs = new DataContractJsonSerializer(typeof(T));
public static object DeserializeObject(string serializedData, Encoding encoding)
{
byte[] data = encoding.GetBytes(serializedData);
MemoryStream sr = new MemoryStream(data);
return xs.ReadObject(sr);
}
public static string SerializeObject(T obj, Encoding encoding)
{
MemoryStream ms = new MemoryStream();
xs.WriteObject(ms, obj);
byte[] data = ms.ToArray();
return encoding.GetString(data);
}
}
public static T DeserializeObject<T>(this string serializedData)
{
return (T)JsonSerializer<T>.DeserializeObject(serializedData, Encoding.Default);
}
public static string SerializeObject<T>(this T obj)
{
return JsonSerializer<T>.SerializeObject(obj, Encoding.Default);
}
}
样品:
class Program
{
static void Main()
{
try
{
string json = "{\"Questions\": [{ \"Question\": \"Who was the Chola King who brought Ganga from North to South?\", \"CorrectAnswer\": 1, \"Answers\": [ { \"Answer\": \"Raja Raja Chola\" }, { \"Answer\": \"Rajendra Chola\" }, { \"Answer\": \"Parantaka\" }, { \"Answer\": \"Mahendra\" } ] }, { \"Question\": \"The writ of 'Habeas Corpus' is issued in the event of:\", \"CorrectAnswer\": 2 , \"Answers\": [{ \"Answer\": \"Loss of Property\" }, { \"Answer\": \"Refund of Excess Taxes\" }, { \"Answer\": \"Wrongful Police Detention\" }, { \"Answer\": \"Violation of the Freedom of Speech\" }] }]}}";
QuestionsRepository newRepository = json.DeserializeObject<QuestionsRepository>();
for (int i = 0; i < newRepository.Questions.Count; i++)
{
Console.WriteLine("{0}", newRepository.Questions[i].Question);
int count = 1;
foreach (var answer in newRepository.Questions[i].Answers)
{
Console.WriteLine("\t{0}) {1} ({2})", count, answer.Answer, newRepository.Questions[i].CorrectAnswer == count ? "+" : "-");
count++;
}
}
}
catch (SerializationException serEx)
{
Console.WriteLine(serEx.Message);
Console.WriteLine(serEx.StackTrace);
}
}
}
P.S。:类必须是顶级enities,默认构造函数可用(数据序列化程序的可见,可访问类)将在DataContractJsonSerializer中使用