1
  1. List item

I converted and stripped all the namespaces from the xml file and converted to Json object and below is my file in jobject This is what I do to get the returned Env object but it returned null.

Can you please help pointing out what were missing in my models? I have been doing different routes without success :(

 var jsonObject =
    JObject.Parse(Newtonsoft.Json.JsonConvert.SerializeXNode(XDocument.Parse(responseRet)));
                var Env= Newtonsoft.Json.JsonConvert.DeserializeObject<Envelope>(jsonObject.ToString());

     ```


  
  1. This is my classes:

    public class SearchRecords_V2 { public List<SearchTXRecord_V2> SearchTXRecord_V2 { get; set; } }

    public class SearchTXRecord_V2 { [DataMember] public int SearchTXRecordID { get; set; } [DataMember] public string FirstName { get; set; } [DataMember] public string MiddleName { get; set; } [DataMember] public string LastName { get; set; } [DataMember] public string Suffix { get; set; } [DataMember] public string SSN { get; set; } [DataMember] public string Address { get; set; } [DataMember] public string City { get; set; } [DataMember] public string State { get; set; } [DataMember] public string PostalCode { get; set; } [DataMember] public string ComponentID { get; set; } [DataMember] public string Status { get; set; } [DataMember] public string Created { get; set; } }

1
  • Hello and welcome to SO! The above json isn't valid. What I would do, is create c# classes that would represent this json and then you could deserialize this easily and perform any search you want. There are many json to c# online generators that would help you create these classes, here are a few: JSON2CSharp and QuickType. Can you tell us how the json is serialized, are you getting it this way and or are you creating it? Commented Mar 9, 2021 at 3:49

1 Answer 1

0

By using Newtonsoft.json library you can parse it to a dot net model

Model:

namespace ConvertFromJson
{
    public partial class DemoJsonObj
    {
        public Employees Employees { get; set; }
    }

    public partial class Employees
    {
        public List<Employee> Employee { get; set; }
    }

    public partial class Employee
    {
        public long Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public Uri Photo { get; set; }
        public string SearchType { get; set; }
        public SearchRecords SearchRecords { get; set; }
    }

    public partial class SearchRecords
    {
        public List<SearchTestRecord> SearchTestRecord { get; set; }
    }

    public partial class SearchTestRecord
    {
        public long Field1 { get; set; }
        public string Field2 { get; set; }
        public RecordType RecordType { get; set; }
    }

    public partial class RecordType
    {
        public NativeRecordNumber NativeRecordNumber { get; set; }
    }

    public partial class NativeRecordNumber
    {
        public Uri Xmlns { get; set; }
        public string Text { get; set; }
    }
}

In your code

var json = "{"employees":{"employee":[{"id":"1","firstName":"Tom","lastName":"Cruise","photo":"https://jsonformatter.org/img/tom-cruise.jpg","SearchType":"type1"},{"id":"2","firstName":"Maria","lastName":"Sharapova","photo":"https://jsonformatter.org/img/Maria-Sharapova.jpg","SearchType":"type1","SearchRecords":{"SearchTestRecord":[{"field1":"128181","field2":"TESTCASE","RecordType":{"NativeRecordNumber":{"@xmlns":"http://schemas.link.com/searchType/1.0.0/","#text":"MPXXXX2011"}}}]}},{"id":"3","firstName":"Robert","lastName":"Downey Jr.","photo":"https://jsonformatter.org/img/Robert-Downey-Jr.jpg","SearchType":"type2"}]}}"
DemoJsonObj obj = Newtonsoft.Json.JsonConvert.DeserializeObject<DemoJsonObj>(json);
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you, I have followed your instruction here. However, List<SearchTestRecord> returned 0 where it should returned the record underneath it. Any idea of what was missing?
you should mark accepted if answer works. these things should be taken care of. 1. Nullable types should be with ? ex: public long? Field1. 2. JSON property name should match exactly with the property name of the model object.
No. The SearchTestRecord list kept returning 0 which I found it strange and not sure what was missing. The nullable fields should not be the case as the file passed in is Xml. Any thoughts of how to capture or get the list populated? I appreciate any help in advance.
please post the JSON that you received, which you parsing to dot net model.
I don't know where to post the code so I have edited the question and reposted in XML as the file that was passed in and the objects I have created to support that. As I explained, the issue I currently have is the list of SearchTestRecord returned 0 instead of 1 based off the xml file. not sure what else I have missed to get the things mapped/ populated correctly. :-(
|

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.