5

I have a Json String Like this ,, and i wana load it in C# Array .. When i try to do this i am getting Exception

My String :

 {
"customerInformation":
[
  {
     "customerId":"123",
     "CustomerName":"",
     "Age":39,
     "Gender":"Male",
     "StudyInfo":[
        {
           "Modality":"XRAY",
           "StudyName":"Test Name",
           "ModalityId":"1",
           "StudyID":"10923",
           "visitid":41549113,
           "billingId":"456",
           "RegDate":"mm/dd/yyyy",
           "uploaded":"1",
           "groupid":"1"

        },
        {
           "Modality":"XRAY",
           "StudyName":"CT Test Name",
           "ModalityId":"1",
           "StudyID":"10924",
           "visitid":41549113,
           "billingId":"459",
           "RegDate":"mm/dd/yyyy",
           "uploaded":"1",
           "groupid":"1"

        }
     ]
  },

  {
     "customerId":"928",
     "CustomerName":"",
     "Age":49,
     "Gender":"FeMale",
     "StudyInfo":[
        {
           "Modality":"XRAY",
           "StudyName":"Test Name",
           "ModalityId":"1",
           "StudyID":"10923",
           "visitid":41549113,
           "billingId":"456",
           "RegDate":"mm/dd/yyyy",
           "uploaded":"1",
           "groupid":"1"
        },
        {
           "Modality":"XRAY",
           "StudyName":"CT Test Name",
           "ModalityId":"1",
           "StudyID":"10924",
           "visitid":41549113,
           "billingId":"459",
           "RegDate":"mm/dd/yyyy",
           "uploaded":"1",
           "groupid":"1"
        }
     ]
  }

]

 }

My Code :

public class Attributes
{


    public string[] customerId { get; set; }
    public string[] CustomerName { get; set; }
    public string[] Age { get; set; }
    public string[] Gender { get; set; }
    public string[] StudyInfo { get; set; }
    public string[] Modality { get; set; }
    public string[] StudyName { get; set; }
    public string[] ModalityId { get; set; }
    public string[] StudyID { get; set; }
    public string[] visitid { get; set; }
    public string[] billingId { get; set; }
    public string[] RegDate { get; set; }
    public string[] uploaded { get; set; }
}

public class DataJsonAttributeContainer
{
    public List<Attributes> attributes { get; set; }
}

 public static T DeserializeFromJson<T>(string json)
    {
        T deserializedProduct = JsonConvert.DeserializeObject<T>(json);
       return deserializedProduct;
    }

   public void testing()
    {
  var container = DeserializeFromJson<DataJsonAttributeContainer>(JsonString);

    }

"This returns Null"

And I have tried this also

            JArray jArray = (JArray)JsonConvert.DeserializeObject(JsonStr);
            dynamic dynObj1 = jArray.OrderByDescending(x => x["customerId"]);

Both Cases got Failed... How to load this .. I am using Newtonsoft.Json Dll

2
  • Try a smaller sample first and work your way up. Some hints a long the way is that the object you are converting to should have the same structure as the json, which it doesn't in your sample code. Commented Jul 5, 2013 at 5:24
  • In your json the array is called 'customerInformation' but in your container class it's called 'attributes'. Try using the same name in both. Commented Jul 5, 2013 at 5:47

2 Answers 2

9

The way you generated you objects is wrong, It should be something like:

public class StudyInfo
{
    public string Modality { get; set; }
    public string StudyName { get; set; }
    public string ModalityId { get; set; }
    public string StudyID { get; set; }
    public int visitid { get; set; }
    public string billingId { get; set; }
    public string RegDate { get; set; }
    public string uploaded { get; set; }
    public string groupid { get; set; }
}

public class CustomerInformation
{
    public string customerId { get; set; }
    public string CustomerName { get; set; }
    public int Age { get; set; }
    public string Gender { get; set; }
    public List<StudyInfo> StudyInfo { get; set; }
}

public class RootObject
{
    public List<CustomerInformation> customerInformation { get; set; }
}

By the way, you can try json2charp, for stuff like this, it's pretty awesome.

Sign up to request clarification or add additional context in comments.

Comments

1

user2552410!

Maybe you need to change your class structure. You can work with List<>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleApplication2.TestService;
using Newtonsoft.Json;

namespace ConsoleApplication2
{
    public class Customer
    {
        public string customerId { get; set; }
        public string CustomerName { get; set; }
        public string Age { get; set; }
        public string Gender { get; set; }
        public StudyInfoType[] StudyInfo { get; set; }
        public string visited { get; set; }
        public string billingId { get; set; }
        public string RegDate { get; set; }
        public string uploaded { get; set; }
    }

    public class StudyInfoType
    {
           string Modality {get; set;}
           string StudyName {get; set;}
           string ModalityId {get; set;}
           string StudyID {get; set;}
           string visitid {get; set;}
           string billingId {get; set;}
           string RegDate {get; set;}
           string uploaded {get; set;}
           string groupid { get; set; }
    }


    class Program
    {
        static void Main()
        {
            var temp = CustomerInfo(@"[{ 'customerId':'123', 'CustomerName':'', 'Age':39,'Gender':'Male','StudyInfo':[{'Modality':'XRAY','StudyName':'Test Name','ModalityId':'1','StudyID':'10923','visitid':41549113,'billingId':'456','RegDate':'mm/dd/yyyy','uploaded':'1','groupid':'1'},{'Modality':'XRAY','StudyName':'CT Test Name','ModalityId':'1','StudyID':'10924','visitid':41549113,'billingId':'459','RegDate':'mm/dd/yyyy','uploaded':'1','groupid':'1'}]},{'customerId':'928','CustomerName':'','Age':49,'Gender':'FeMale','StudyInfo':[{'Modality':'XRAY','StudyName':'Test Name','ModalityId':'1','StudyID':'10923','visitid':41549113,'billingId':'456','RegDate':'mm/dd/yyyy','uploaded':'1','groupid':'1'},{ 'Modality':'XRAY','StudyName':'CT Test Name','ModalityId':'1','StudyID':'10924','visitid':41549113,'billingId':'459','RegDate':'mm/dd/yyyy','uploaded':'1','groupid':'1' } ] } ]");
        }

        public static List<Customer> CustomerInfo(string json)
        {
            var n = JsonConvert.DeserializeObject(json, new JsonSerializerSettings
            {
                ObjectCreationHandling = ObjectCreationHandling.Replace
            });
            return JsonConvert.DeserializeObject<List<Customer>>(json);
        }
    }
}

2 Comments

But string should start with "customerInformation": you directly added Customer id
Yes, it's right. To my mind, it will be more better to create two more tables. One for "CustomerInformstion" and one for "Visit Detailes". The table "CustomerInformation" will contain List<Visit Detailes"

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.