I'm building an app for modifying existing json files. I have made all the classes that correspond to my json code. Most of the times I need to copy whole json collection and then just change a few things (like id, index, etc.).
I successfully copy and add whole collection and when I try to change that something in that collection it makes changes in the collection from which I did the copying. Could you please tell me what am I doing wrong?
private void addFlightButton_Click(object sender, RoutedEventArgs e) {
var originDestinationGroup = jsonObject.search.originDestinationGroup;
OriginDestinationGroup oDGitem = originDestinationGroup.Last();
var departureDateGroup = oDGitem.departureDateGroup;
DepartureDateGroup dDGitem = departureDateGroup.Last();
var originDestinationOptionRef = dDGitem.originDestinationOptionRef;
OriginDestinationOptionRef oDORitem = originDestinationOptionRef.Last();
originDestinationOptionRef.Add(oDORitem);
char uriNumber = oDORitem.uri[oDORitem.uri.Length - 1];
string uriString = oDORitem.uri.TrimEnd(oDORitem.uri[oDORitem.uri.Length - 1]);
int idNumber = Convert.ToInt32(oDORitem.id);
int indexNumber = oDORitem.index;
uriNumber++;
idNumber++;
indexNumber++;
OriginDestinationOptionRef oDORitemModify = originDestinationOptionRef.Last();
oDORitemModify.uri = uriString + uriNumber.ToString();
oDORitemModify.id = idNumber.ToString();
oDORitemModify.index = indexNumber;
}
json Response - Not modified:
"search": {
"originDestinationGroup": [{
"departureDateGroup": [{
"originDestinationOptionRef": [{
"uri": "option/1",
"id": "1",
"index": 0
},
{
"uri": "option/2",
"id": "2",
"index": 1
}
]
json Response - Modified by my code:
"search": {
"originDestinationGroup": [{
"departureDateGroup": [{
"originDestinationOptionRef": [{
"uri": "option/1",
"id": "1",
"index": 0
},
{
"uri": "option/3",
"id": "3",
"index": 2
},
{
"uri": "option/3",
"id": "3",
"index": 2
}
]
json Response - How it suppose to be modified:
"search": {
"originDestinationGroup": [{
"departureDateGroup": [{
"originDestinationOptionRef": [{
"uri": "/option/1",
"id": "1",
"index": 0
},
{
"uri": "option/2",
"id": "2",
"index": 1
},
{
"uri": "option/3",
"id": "3",
"index": 2
}
]
Classes:
public class Search {
public List<OriginDestinationGroup> originDestinationGroup { get; set; }
}
public class OriginDestinationGroup {
public List<DepartureDateGroup> departureDateGroup { get; set; }
}
public class DepartureDateGroup {
public List<OriginDestinationOptionRef> originDestinationOptionRef { get; set; }
}
public class OriginDestinationOptionRef {
public string uri { get; set; }
public string id { get; set; }
public int index { get; set; }
}
Thank you in advance.