Search code examples
c#jsonjson-deserialization

How to read specific Json object from Json string in C#?


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?


Solution

  • 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;
    }