2

I am using a function to read from a json file and reaturn a list of class to the UI. My class is

 public class UseCase
    {
        public string UseCaseId { get; set;}
        public string UseCaseDescription { get; set;}
    }

The function is like this

 List<UseCase> usecaseList = new List<UseCase>();
ReadUsecaseContentFronJsonFile1();
         usecaseList = UseCaseList();

The UseCaseList() is

private List<UseCase> UseCaseList()
        {
            List<UseCase> usecaseList = new List<UseCase>();
            foreach (var value in deserializedData1.Values)
            {
                UseCase objUC = new UseCase();

                objUC.UseCaseId = value.UseCaseId;
                objUC.UseCaseDescription = value.UseCaseDescription;
                usecaseList.Add(objUC);
            }
            return usecaseList;//isSigRequired;
        }

private void ReadUsecaseContentFronJsonFile1()
        {
            deserializedData1 = ReadFromJsonFile1<Dictionary<string, UseCase>>();
        }

The function to read from json file is like this

 protected static T ReadFromJsonFile1<T>() where T : new()
        {
            TextReader JsonReader = null;
            try
            {
                var filePath = ConfigurationManager.AppSettings.Get("UsecaseListPath");
                if (File.Exists(filePath))
                {
                    JsonReader = new StreamReader(filePath);
                    var fileContents = JsonReader.ReadToEnd();
                    return JsonConvert.DeserializeObject<T>(fileContents);
                }
                else
                {
                    return new T();
                }
            }
            finally
            {
                if (JsonReader != null)
                    JsonReader.Close();
            }
        }

I am not getting the json file values in (deserializedData1). What could be the issue??

My json file is:

{

    "UseCases": {
        "UseCase": [{
            "UseCaseId": "CCS01",
            "UseCaseDescription": "Add Device to  CHF device log"
        }, {
            "UseCaseId": "CCS02",
            "UseCaseDescription": "Remove device from  CHF device log"
        }, {
            "UseCaseId": "CCS03",
            "UseCaseDescription": "Restore CHF Device  Log"
        }
}

3 Answers 3

1

Look at the beginning of the JSON document:

{
    "UseCases": {
        "UseCase": [{

It says that you have a root object which contains an object which contains a object which contains a list ;) Or more nicely:

  1. You have a root object
  2. The root object contains a field named "UseCases" which is an object.
  3. That field "UseCases" contains a property "UseCase" which is a collection

Code:

// For the root object which contains the "UseCases" field
public class RootObject
{
    public UseCasesContainer UseCases { get; set; }
}

// For the "UsesCases" field, the tricky thing is that 
// it's not a collection but an object
public class UseCasesContainer
{

    // ..which contains a collection field.
    public Collection<UsesCase> UsesCase { get; set; }
}

The collection itself contains UseCase objects like you specified.

Thus to deserialize it you can use json.net like:

var rootObject = JsonConvert.DeserializeObject<RootObject>(yourJson);

//don't be fooled by the property name, this is the collection
var allUseCases = rootObject.UseCases.UseCase; 

If your JSON only should contain use cases you could have created a document without fields:

[
        {
            "UseCaseId": "CCS01",
            "UseCaseDescription": "Add Device to  CHF device log"
        }, {
            "UseCaseId": "CCS02",
            "UseCaseDescription": "Remove device from  CHF device log"
        }, {
            "UseCaseId": "CCS03",
            "UseCaseDescription": "Restore CHF Device  Log"
        }
]

.. which could just have been deserialized as:

var useCases = JsonConvert.DeserializeObject<IList<UseCase>>(json);
Sign up to request clarification or add additional context in comments.

Comments

0

This works:

static void Main(string[] args)
{
    var fileName = @"c:\temp\test.json";
    var reader = new StreamReader(fileName);
    var fileContent = reader.ReadToEnd();

    var useCases = JsonConvert.DeserializeObject<Dictionary<string, List<UseCase>>>(fileContent);
    Console.WriteLine("Done.");
}

Provided your JSON is modified to this (as pointed out in the previous post):

{
    "UseCases": [{        
            "UseCaseId": "CCS01",
            "UseCaseDescription": "Add Device to  CHF device log"
        }, {
            "UseCaseId": "CCS02",
            "UseCaseDescription": "Remove device from  CHF device log"
        }, {
            "UseCaseId": "CCS03",
            "UseCaseDescription": "Restore CHF Device  Log"
        }]
}

1 Comment

yes I am getting the list values in useCases. But how to convert it into List<Usecase>???
0

I can see the JSON format is wrong.. replace the json with below

Click here to view the Image

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.