Reputation: 6066
Following is the JSON
string:
{
"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": ""
}
}
}
Following Reads ALLJSON
in Key:Value
and displays in table format But i need only specific object i.e FMI section of json string only:
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;
}
Problem:
I want to read ALL the content of "fmi" section only. and display in key:Value. table format
NOTE:
I am using Framework 3.5 hence cant use dynamic
keyword.
Any idea?
Upvotes: 1
Views: 2105
Reputation: 162
This can be solved accurately with this method, just keep in mind, you will need 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
*/
Here's the JSON Example
{
"name": "itzplayz"
}
Now you can get the value of the JSON object in the string called value
Upvotes: 0
Reputation: 56556
I'd recommend that you work with it as a JObject
directly; this is the class you'd be using behind-the-scenes if you were using dynamic
. Also, you need to separate out the selection of fmi
from the normal path done in the recursive calls: here I have it in 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;
}
Upvotes: 1
Reputation: 29846
Since you are not deserializing this into a "strong typed" object you can do something like this:
var fmi = JsonConvert.DeserializeObject<Dictionary<string,object>>(str)["fmi"];
var keys = JsonConvert.DeserializeObject<Dictionary<string, object>>(fmi.ToString());
Upvotes: 1