以下是JSON
字符串:
{
"ios_info": {
"serialNumber": "F2LLMBNJFFF",
"imeiNumber": "01388400413235",
"meid": "",
"iccID": "8901410427640096045",
"firstUnbrickDate": "11/27/13",
"lastUnbrickDate": "11/27/13",
"unbricked": "true",
"unlocked": "false",
"productVersion": "7.1.2",
"initialActivationPolicyID": "23",
"initialActivationPolicyDetails": "US AT&T Puerto Rico and US Virgin Islands Activation Policy",
"appliedActivationPolicyID": "23",
"appliedActivationDetails": "US AT&T Puerto Rico and US Virgin Islands Activation Policy",
"nextTetherPolicyID": "23",
"nextTetherPolicyDetails": "US AT&T Puerto Rico and US Virgin Islands Activation Policy",
"macAddress": "ACFDEC6C988A",
"bluetoothMacAddress": "AC:FD:EC:6C:98:8B",
"partDescription": "IPHONE 5S SPACE GRAY 64GB-USA"
},
"fmi": {
"@attributes": {
"version": "1",
"deviceCount": "1"
},
"fmipLockStatusDevice": {
"@attributes": {
"serial": "F2LLMBNJFFFQ",
"imei": "013884004132355",
"isLocked": "true",
"isLost": "false"
}
}
},
"product_info": {
"serialNumber": "F2LLMBNJFFFQ",
"warrantyStatus": "Apple Limited Warranty",
"coverageEndDate": "11/25/14",
"coverageStartDate": "11/26/13",
"daysRemaining": "498",
"estimatedPurchaseDate": "11/26/13",
"purchaseCountry": "United States",
"registrationDate": "11/26/13",
"imageURL": "http://service.info.apple.com/parts/service_parts/na.gif",
"explodedViewURL": "http://service.info.apple.com/manuals-ssol.html",
"manualURL": "http://service.info.apple.com/manuals-ssol.html",
"productDescription": "iPhone 5S",
"configDescription": "IPHONE 5S GRAY 64GB GSM",
"slaGroupDescription": "",
"contractCoverageEndDate": "11/25/15",
"contractCoverageStartDate": "11/26/13",
"contractType": "C1",
"laborCovered": "Y",
"limitedWarranty": "Y",
"partCovered": "Y",
"notes": "Covered by AppleCare+ - Incidents Available",
"acPlusFlag": "Y",
"consumerLawInfo": {
"serviceType": "",
"popMandatory": "",
"allowedPartType": ""
}
}
}
在JSON
中读取所有 Key:Value
并以表格格式显示但我只需要特定对象,即 FMI 部分仅限json字符串:
private string GetKeyValuePairs(string jsonString)
{
var resDict = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonString);
string sdict = string.Empty;
string fmitxt = string.Empty;
string fmitxt2 = string.Empty;
foreach (string key in resDict.Keys)
{
sdict += "<tr><td> " + key + "</td> " + (resDict[key].GetType() == typeof(Newtonsoft.Json.Linq.JObject) ? "<td>" + GetKeyValuePairs(resDict[key].ToString()) + "</td></tr>" : "<td>" + resDict[key].ToString() + "</td></tr>");
}
return sdict;
}
问题:
我想阅读&#34; fmi&#34;的所有内容仅限部分。并以键:值。表格式
显示 注意:
我使用 Framework 3.5 因此无法使用dynamic
关键字。
有什么想法吗?
答案 0 :(得分:1)
由于您没有将其反序列化为“强类型”对象,因此可以执行以下操作:
var fmi = JsonConvert.DeserializeObject<Dictionary<string,object>>(str)["fmi"];
var keys = JsonConvert.DeserializeObject<Dictionary<string, object>>(fmi.ToString());
答案 1 :(得分:1)
我建议你直接使用它JObject
;如果您使用dynamic
,这是您在幕后使用的课程。此外,您需要将fmi
的选择与递归调用中的正常路径分开:这里我将其放在Main
中。
void Main()
{
var resObj = JsonConvert.DeserializeObject<JObject>(jsonString);
var result = GetKeyValuePairs((JObject)resObj["fmi"]);
}
private string GetKeyValuePairs(JObject resObj)
{
string sDict = string.Empty;
string fmitxt = string.Empty;
string fmitxt2 = string.Empty;
foreach (var pair in resObj)
{
sDict += "<tr><td> " + pair.Key + "</td>";
if (pair.Value is JObject)
sDict += "<td>" + GetKeyValuePairs((JObject)pair.Value) + "</td></tr>";
else
sDict += "<td>" + pair.Value.ToString() + "</td></tr>";
}
return sDict;
}
答案 2 :(得分:0)
使用此方法可以准确解决此问题,请记住,您将需要Newtonsoft.Json;
//using Newtonsoft.Json;
// using System.IO;
string dataPath = @"myfile.json"; // Location of the json
string jsonData = "";
using (StreamReader r = new StreamReader(Path.Combine(dataPath)))
{
string json = r.ReadToEnd();
jsonData = json;
}
dynamic obj = JsonConvert.DeserializeObject(jsonData);
string value = obj["name"]; // here name is the object name holding the required data
Console.WriteLine(value);
Console.ReadLine();
/*
* OUTPUT
* itzplayz
*/
这是JSON示例
{
"name": "itzplayz"
}
现在您可以在名为value的字符串中获取JSON对象的值