1

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?

3 Answers 3

1

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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

What is the word "Dump()" is it a keyword in 3.5? I am not getting your code! Throws error! :(
@SHEKHARSHETE Oops, that was a thing from my debugging/developing in LINQPad. It just let me see the results of that method call. You can remove it.
Hey @Tim S! you are simply great..! :) It works! after modifying at certain extent :) Thanks a lot!
1

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());

4 Comments

have u tried this? it gives error at line: resDict["fmi"].Keys. Error:Object does not contain defination for Keys. :(
Not in formatted way as i need! but still you deserve +1 and appreciate your time spending for me! :)
I would create a strong type object for the fmi section and deserialize the content into it. It's hard to work with dynamics\dictionaries\jsons in the current way.
Yes i agree! This approach i have choosen becoz the JSON string contents may vary. First i have used the JSON to C# Class converter but the JSON fails to compile so...:)
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.