如何从Json字符串中读取特定的Json对象
本文关键字:Json 对象 读取 字符串 | 更新日期: 2023-09-27 18:13:30
以下是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": ""
}
}
}
以下读取Key:Value
中所有 JSON
并以表格式显示,但我只需要特定对象即FMIsection 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;
}
问题:
我想读取"fmi"部分的所有内容,仅。并显示在key:Value中。表格式
注意:我正在使用框架3.5,因此不能使用dynamic
关键字。任何想法?
我建议你直接使用它作为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;
}
由于您没有将其反序列化为"强类型"对象,因此您可以这样做:
var fmi = JsonConvert.DeserializeObject<Dictionary<string,object>>(str)["fmi"];
var keys = JsonConvert.DeserializeObject<Dictionary<string, object>>(fmi.ToString());
这可以用这种方法精确地解决,只需记住,您将需要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