Obisously I'm still learning how to create valid JSON but I'm struggling with this one. I was going to make a JSON file that I will create a list out of in my .Net application. What I'm looking to do is have a file of payroll codes specific for different companies. For each company I'm looking to have an array of codes that when I find the code in the array I would then use the Absence type as the friendly name. When I run it through a JSON validator it is complaining about the array brackets just after the company name. I'm betting it's something easy and intuitive to someone that knows JSON but I'm missing what I need to do. When I try to search on JSON and Arrays I can't find anything on how to use a nested array. Can someone give me an example or point me to information about nested arrays?
[
{
"Company1": {
[
{
"PayCode": "SCK",
"AbsenceType": "Illness"
},
{
"PayCode": "VAC",
"AbsenceType": "Vacation"
},
{
"PayCode": "BRV",
"AbsenceType": "Bereavement"
},
{
"PayCode": "JUR",
"AbsenceType": "Jury Duty"
},
{
"PayCode": "PER",
"AbsenceType": "Personal"
}
]
},
"Company2": {
[
{
"PayCode": "SCK",
"AbsenceType": "Sick"
},
{
"PayCode": "VAC",
"AbsenceType": "Vacation"
},
{
"PayCode": "BRV",
"AbsenceType": "Bereavement"
},
{
"PayCode": "JUR",
"AbsenceType": "Jury Duty"
},
{
"PayCode": "PER",
"AbsenceType": "Personal"
},
{
"PayCode": "PRNU",
"AbsenceType": "Personal"
}
]
}
}
]
CodePudding user response:
JSON is a key/value format. Your issue is that the arrays are inside of curly brackets ({}) which makes them a key, with no value.
"Company1": { <-- Curly Bracket (Implies that data is in form of a hash (key/value) )
[
{
"PayCode": "SCK",
"AbsenceType": "Illness"
},
{
"PayCode": "VAC",
"AbsenceType": "Vacation"
},
{
"PayCode": "BRV",
"AbsenceType": "Bereavement"
},
{
"PayCode": "JUR",
"AbsenceType": "Jury Duty"
},
{
"PayCode": "PER",
"AbsenceType": "Personal"
}
] <-- key ends with no value.
},
I think you need to store the array for company one as just the array like so:
"Company1": [ <-- The array is the value for company1
{
"PayCode": "SCK",
"AbsenceType": "Illness"
},
{
"PayCode": "VAC",
"AbsenceType": "Vacation"
},
{
"PayCode": "BRV",
"AbsenceType": "Bereavement"
},
{
"PayCode": "JUR",
"AbsenceType": "Jury Duty"
},
{
"PayCode": "PER",
"AbsenceType": "Personal"
}
] <-- key ends with no value.
This would be your whole file:
[
{
"Company1":[
{
"PayCode": "SCK",
"AbsenceType": "Illness"
},
{
"PayCode": "VAC",
"AbsenceType": "Vacation"
},
{
"PayCode": "BRV",
"AbsenceType": "Bereavement"
},
{
"PayCode": "JUR",
"AbsenceType": "Jury Duty"
},
{
"PayCode": "PER",
"AbsenceType": "Personal"
}
],
"Company2":[
{
"PayCode": "SCK",
"AbsenceType": "Sick"
},
{
"PayCode": "VAC",
"AbsenceType": "Vacation"
},
{
"PayCode": "BRV",
"AbsenceType": "Bereavement"
},
{
"PayCode": "JUR",
"AbsenceType": "Jury Duty"
},
{
"PayCode": "PER",
"AbsenceType": "Personal"
},
{
"PayCode": "PRNU",
"AbsenceType": "Personal"
}
]
}
]
CodePudding user response:
Matthias Lee json is valid , but you will not get much use of it since it can be only deserialized as a list of dictionaries. For a practical use you will probably need to convert it to list of c# classes. So it is better to create somethig like this
[
{
"Name": "Company1",
"Accounts": [
{
"PayCode": "BRV",
"AbsenceType": "Bereavement"
},
{
"PayCode": "JUR",
"AbsenceType": "Jury Duty"
}
]
},
{
"Name": "Company2",
"Accounts": [
{
"PayCode": "SCK",
"AbsenceType": "Sick"
},
{
"PayCode": "VAC",
"AbsenceType": "Vacation"
}
]
}
]
how to use
List<Company> companies=JsonConvert.DeserializeObject<List<Company>>(json);
public class Account
{
public string PayCode { get; set; }
public string AbsenceType { get; set; }
}
public class Company
{
public string Name {get; set;}
public List<Account> Accounts { get; set; }
}
