问题描述
- C# 求这个通过Newtonsoft.Json解析json
-
{
"isSuccess": true,
"errorMsg": "",
"timeStamp": "14",
"nonce": "13090713",
"sign": "3B732684B206EB3CE324E1E0FD4FAD368A638BD4",
"data": {
"msg": "",
"inkey": [
{
"paper_uid": "4bc8ebf2-7b1a-4952-a437-827810eaf78b",
"paper_name": "sater试卷",
"question_num": 6,
"total_score": "35",
"begin_time": "2015-03-02 16:31:16",
"allow_exam_time": "0",
"exam_grade_uid": "BD1972C5-6614-41E2-A270-D9C9DB1D509B",
"wait_time": 5,
"allow_showAnswer": "N",
"show_preQuestion": "Y",
"exam_do_mode_code": "46209b31-3154-4f1a-88e0-c286ba3fcdc9"
},
[
{
"paper_node_name": "单选题",
"question_num": "2",
"question_score": "0",
"total_score": "5"
},
[
{
"question_uid": "f86fa985-7295-4f4f-892f-d0e910b36459",
"question_base_typec_code": "single",
"paper_question_score": "5",
"question_text": "选什么?A",
"standard_answer": "A",
"question_exam_time": 20
},
[
{
"select_answer_value": "A",
"select_answer_text": "1111"
},
{
"select_answer_value": "B",
"select_answer_text": "222"
},
{
"select_answer_value": "C",
"select_answer_text": "333"
},
{
"select_answer_value": "D",
"select_answer_text": "4444"
}
]
],
[
{
"question_uid": "a7c04502-32db-4918-b037-4b42a0034720",
"question_base_typec_code": "single",
"paper_question_score": "0",
"question_text": "单选题___选B",
"standard_answer": "B",
"question_exam_time": 20
},
[
{
"select_answer_value": "A",
"select_answer_text": "qqqqqq"
},
{
"select_answer_value": "B",
"select_answer_text": "wwwww"
},
{
"select_answer_value": "C",
"select_answer_text": "eeeee"
},
{
"select_answer_value": "D",
"select_answer_text": "rrrrrrrr"
}
]
]
],
[
{
"paper_node_name": "多选题",
"question_num": "1",
"question_score": "0",
"total_score": "8"
},
[
{
"question_uid": "6465e790-02db-41d5-a387-06156196c8f1",
"question_base_typec_code": "multi",
"paper_question_score": "8",
"question_text": "选什么?AB",
"standard_answer": "A|B",
"question_exam_time": 20
},
[
{
"select_answer_value": "A",
"select_answer_text": "aaaa"
},
{
"select_answer_value": "B",
"select_answer_text": "bbb"
},
{
"select_answer_value": "C",
"select_answer_text": "cccc"
},
{
"select_answer_value": "D",
"select_answer_text": "dddd"
}
]
]
],
[
{
"paper_node_name": "判断题",
"question_num": "1",
"question_score": "0",
"total_score": "12"
},
[
{
"question_uid": "48f09eaa-9a44-4960-8610-7ec138865943",
"question_base_typec_code": "judge",
"paper_question_score": "12",
"question_text": "判断---对的,",
"standard_answer": "Y",
"question_exam_time": 20
},
[
{
"select_answer_value": "N",
"select_answer_text": "错误"
},
{
"select_answer_value": "Y",
"select_answer_text": "正确"
}
]
]
],
[
{
"paper_node_name": "填空题",
"question_num": "1",
"question_score": "0",
"total_score": "5"
},
[
{
"question_uid": "72a40be5-b93a-46a0-a3e2-d3a82e4d6d7f",
"question_base_typec_code": "fill",
"paper_question_score": "5",
"question_text": "填空___ok",
"standard_answer": "ok",
"question_exam_time": 20
},
[]
]
],
[
{
"paper_node_name": "问答题",
"question_num": "1",
"question_score": "0",
"total_score": "5"
},
[
{
"question_uid": "4d20a8a6-81ae-426f-bdfe-83c9d79848c1",
"question_base_typec_code": "answer",
"paper_question_score": "5",
"question_text": "问答题?嗯",
"standard_answer": "嗯",
"question_exam_time": 20
},
[]
]
]
]
}
}
解决方案
Newtonsoft.Json序列化和反序列
这里下载:http://www.newtonsoft.com/products/json/
安装:
1.解压下载文件,得到Newtonsoft.Json.dll
2.在项目中添加引用..
序列化和反序列在.net项目中:
Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };
string output = javascriptConvert.SerializeObject(product);
//{
// "Name": "Apple",
// "Expiry": new Date(1230422400000),
// "Price": 3.99,
// "Sizes": [
// "Small",
// "Medium",
// "Large"
// ]
//}
Product deserializedProduct = (Product)javascriptConvert.DeserializeObject(output, typeof(Product));
读取JSON
string jsonText = "['JSON!',1,true,{property:'value'}]";
JsonReader reader = new JsonReader(new StringReader(jsonText));
Console.WriteLine("TokenTypettValueTypettValue");
while (reader.Read())
{
Console.WriteLine(reader.TokenType + "tt" + WriteValue(reader.ValueType) + "tt" + WriteValue(reader.Value))
}
结果显示:
TokenType ValueType Value
StartArray null null
String System.String JSON!
Integer System.Int32 1
Boolean System.Boolean True
StartObject null null
PropertyName System.String property
String System.String value
EndObject null null
EndArray null null
JSON写入
StringWriter sw = new StringWriter();
JsonWriter writer = new JsonWriter(sw);
writer.WriteStartArray();
writer.WriteValue("JSON!");
writer.WriteValue(1);
writer.WriteValue(true);
writer.WriteStartObject();
writer.WritePropertyName("property");
writer.WriteValue("value");
writer.WriteEndObject();
writer.WriteEndArray();
writer.Flush();
string jsonText = sw.GetStringBuilder().ToString();
Console.WriteLine(jsonText);
// ['JSON!',1,true,{property:'value'}]
这里会打印出: ['JSON!',1,true,{property:'value'}].
解决方案二:
参考链接
http://www.cnblogs.com/sbxwylt/archive/2008/12/31/1366199.html