locked
change property name of an object RRS feed

  • Question

  • User-963208184 posted

    Hi All,

    I have an object lets call it Travellers so my classes may look something like this:

    public class Travellers
    {
    .......
    .......
      public IEnumerable<Traveller> Traveller {get; set;}
    }
    
    public class Traveller
    {
     public int Traveller_x_Age {get;set;}
     public string Traveller_x_Sex {get;set;}
    }

    At runtime, I dont know how many travellers there will be. So I just keep adding them to the collection. Later in my code now that I have all my Travellers I want to replace the 'x' in all the properties with a value e.g. For my first traveller the Traveller_x_Age property becomes Traveller_0_Age, my next traveller becomes Traveller_1_Age etc. 

    How can I change the property name?

    Tuesday, June 2, 2015 5:57 AM

Answers

  • User-963208184 posted

    No i'm not confusing my question. The reason I want to do it is that I then serialize the object to Json to send the data to a service. The Service expects travellers to be in the format that I specified. 

    I'm just looking to see if I can manipulate the json string once I've serialised the object, I'm nearly there, but not quite. I have:

    var json = new JavaScriptSerializer().Serialize(_myTravelModel);
                var tt = json.Substring(0, json.IndexOf('[') - 13); //first section
                var tr = json.Substring(json.IndexOf('[') + 2, json.IndexOf(']') - json.IndexOf('[') - 2).Replace('{','\\').Replace('}','\\'); //travellers
                
                var en = json.Substring(json.IndexOf(']') + 1);
                var pass = string.Concat(tt, tr, en);
    

    tt has "{\"Requestor_Id\":\"MY_Request\",\"Policy_Quote_Date\":\"\\/Date(1433155404000)\\/\",\"Policy_Type\":\"Annual\",\"Policy_Start_Date\":\"\\/Date(1437346800000)\\/\",\"Policy_End_Date\":\"\\/Date(1468882800000)\\/\",\"Trip_Duration\":43,\"Cover_For\":\"Couple\",\"Travelling_To\":2,\"Discount_Code\":\"TCUST15\",\"Discount_Core\":\"0.15,0,100\",\"Discount_Addon\":\"\",\"Discount_Medical\":\"0,0,0\",\"No_Of_Travellers\":2,"
    
    tr has "\"Travellers_x_Age\":70,\"Travellers_x_Medical_Score\":0\\,\\\"Travellers_x_Age\":74,\"Travellers_x_Medical_Score\":0\\"
    
    en has
    ",\"Cover_Level\":\"Silver\",\"Cover_Winter_Sports\":false,\"Cover_Business\":true,\"Cover_Cruise\":true,\"Cover_Excess_Waiver\":false,\"Cover_Gadget_L1\":false,\"Cover_Gadget_L2\":false,\"Cover_Gadget_L3\":false,\"Cover_Golf\":false,\"Cover_Natural_Catastrophe\":false,\"Cover_Sports_L1\":false,\"Cover_Sports_L2\":false,\"Cover_Sports_L3\":false,\"Cover_Wedding\":false,\"Request_Documents_By_Post\":false}"
    
    and then pass has:
    "{\"Requestor_Id\":\"MY_Request\",\"Policy_Quote_Date\":\"\\/Date(1433155404000)\\/\",\"Policy_Type\":\"Annual\",\"Policy_Start_Date\":\"\\/Date(1437346800000)\\/\",\"Policy_End_Date\":\"\\/Date(1468882800000)\\/\",\"Trip_Duration\":43,\"Cover_For\":\"Couple\",\"Travelling_To\":2,\"Discount_Code\":\"TCUST15\",\"Discount_Core\":\"0.15,0,100\",\"Discount_Addon\":\"\",\"Discount_Medical\":\"0,0,0\",\"No_Of_Travellers\":2,\"Travellers_x_Age\":70,\"Travellers_x_Medical_Score\":0\\,\\\"Travellers_x_Age\":74,\"Travellers_x_Medical_Score\":0\\,\"Cover_Level\":\"Silver\",\"Cover_Winter_Sports\":false,\"Cover_Business\":true,\"Cover_Cruise\":true,\"Cover_Excess_Waiver\":false,\"Cover_Gadget_L1\":false,\"Cover_Gadget_L2\":false,\"Cover_Gadget_L3\":false,\"Cover_Golf\":false,\"Cover_Natural_Catastrophe\":false,\"Cover_Sports_L1\":false,\"Cover_Sports_L2\":false,\"Cover_Sports_L3\":false,\"Cover_Wedding\":false,\"Request_Documents_By_Post\":false}"
    

    my tr string (will make these more meaningful later) holds 2 values as expected, but when I concat to make pass, pass only has the last value.

    at the moment, I'm just trying to construct the string value to still be a valid Json String (I realised that I need to also flatten the Traveller object within the string) so this is what I'm trying to do at the moment, once I have this, then I will try and parse the 'tr' string

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 2, 2015 7:08 AM

All replies

  • User-1618234021 posted

    You can not change the property names like that. The structure is defined once you declare the class. Only values could be changed not the names of actual data members.

    Tuesday, June 2, 2015 6:27 AM
  • User-434868552 posted

    @billcrawley

    Why would you want to do that?

    are you confusing variable names with variable value?

    Let me explain.

    decades ago, junior programmers would write

    employee1 = "Adam"
    employee2 = "Betty"
    employee3 = "Carrie"

    et cetera

    With your List<Traveller> you simply foreach through your list.

    if you want uniqueness, do something like

    public class Traveller
    {
    public Int32 TravellerID; public Int32 Traveller_Age {get; set;} public String Traveller_Sex {get; set;} }

    you can then foreach through your list assigning values to TravellerID

    N.B.:   a class is a template ... so, even if you achieved your goal, the following would not be allowed to exist at the same time:

    public class Traveller
    {
     public int Traveller_0_Age {get;set;}
     public string Traveller_0_Sex {get;set;}
    }
    public class Traveller
    {
     public int Traveller_1_Age {get;set;}
     public string Traveller_1_Sex {get;set;}
    }
    public class Traveller
    {
     public int Traveller_2_Age {get;set;}
     public string Traveller_2_Sex {get;set;}
    }

    Tuesday, June 2, 2015 6:32 AM
  • User-963208184 posted

    No i'm not confusing my question. The reason I want to do it is that I then serialize the object to Json to send the data to a service. The Service expects travellers to be in the format that I specified. 

    I'm just looking to see if I can manipulate the json string once I've serialised the object, I'm nearly there, but not quite. I have:

    var json = new JavaScriptSerializer().Serialize(_myTravelModel);
                var tt = json.Substring(0, json.IndexOf('[') - 13); //first section
                var tr = json.Substring(json.IndexOf('[') + 2, json.IndexOf(']') - json.IndexOf('[') - 2).Replace('{','\\').Replace('}','\\'); //travellers
                
                var en = json.Substring(json.IndexOf(']') + 1);
                var pass = string.Concat(tt, tr, en);
    

    tt has "{\"Requestor_Id\":\"MY_Request\",\"Policy_Quote_Date\":\"\\/Date(1433155404000)\\/\",\"Policy_Type\":\"Annual\",\"Policy_Start_Date\":\"\\/Date(1437346800000)\\/\",\"Policy_End_Date\":\"\\/Date(1468882800000)\\/\",\"Trip_Duration\":43,\"Cover_For\":\"Couple\",\"Travelling_To\":2,\"Discount_Code\":\"TCUST15\",\"Discount_Core\":\"0.15,0,100\",\"Discount_Addon\":\"\",\"Discount_Medical\":\"0,0,0\",\"No_Of_Travellers\":2,"
    
    tr has "\"Travellers_x_Age\":70,\"Travellers_x_Medical_Score\":0\\,\\\"Travellers_x_Age\":74,\"Travellers_x_Medical_Score\":0\\"
    
    en has
    ",\"Cover_Level\":\"Silver\",\"Cover_Winter_Sports\":false,\"Cover_Business\":true,\"Cover_Cruise\":true,\"Cover_Excess_Waiver\":false,\"Cover_Gadget_L1\":false,\"Cover_Gadget_L2\":false,\"Cover_Gadget_L3\":false,\"Cover_Golf\":false,\"Cover_Natural_Catastrophe\":false,\"Cover_Sports_L1\":false,\"Cover_Sports_L2\":false,\"Cover_Sports_L3\":false,\"Cover_Wedding\":false,\"Request_Documents_By_Post\":false}"
    
    and then pass has:
    "{\"Requestor_Id\":\"MY_Request\",\"Policy_Quote_Date\":\"\\/Date(1433155404000)\\/\",\"Policy_Type\":\"Annual\",\"Policy_Start_Date\":\"\\/Date(1437346800000)\\/\",\"Policy_End_Date\":\"\\/Date(1468882800000)\\/\",\"Trip_Duration\":43,\"Cover_For\":\"Couple\",\"Travelling_To\":2,\"Discount_Code\":\"TCUST15\",\"Discount_Core\":\"0.15,0,100\",\"Discount_Addon\":\"\",\"Discount_Medical\":\"0,0,0\",\"No_Of_Travellers\":2,\"Travellers_x_Age\":70,\"Travellers_x_Medical_Score\":0\\,\\\"Travellers_x_Age\":74,\"Travellers_x_Medical_Score\":0\\,\"Cover_Level\":\"Silver\",\"Cover_Winter_Sports\":false,\"Cover_Business\":true,\"Cover_Cruise\":true,\"Cover_Excess_Waiver\":false,\"Cover_Gadget_L1\":false,\"Cover_Gadget_L2\":false,\"Cover_Gadget_L3\":false,\"Cover_Golf\":false,\"Cover_Natural_Catastrophe\":false,\"Cover_Sports_L1\":false,\"Cover_Sports_L2\":false,\"Cover_Sports_L3\":false,\"Cover_Wedding\":false,\"Request_Documents_By_Post\":false}"
    

    my tr string (will make these more meaningful later) holds 2 values as expected, but when I concat to make pass, pass only has the last value.

    at the moment, I'm just trying to construct the string value to still be a valid Json String (I realised that I need to also flatten the Traveller object within the string) so this is what I'm trying to do at the moment, once I have this, then I will try and parse the 'tr' string

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 2, 2015 7:08 AM