0
Follow the extension:
public static void Rename(this JToken token, string newName)
{
if (token == null)
throw new ArgumentNullException("token", "Cannot rename a null token");
JProperty property;
if (token.Type == JTokenType.Property)
{
if (token.Parent == null)
throw new InvalidOperationException("Cannot rename a property with no parent");
property = (JProperty)token;
}
else
{
if (token.Parent == null || token.Parent.Type != JTokenType.Property)
throw new InvalidOperationException("This token's parent is not a JProperty; cannot rename");
property = (JProperty)token.Parent;
}
var newProperty = new JProperty(newName, property.Value);
property.Replace(newProperty);
}
Follows code:
string json = string.Empty;
try
{
json = JsonConvert.SerializeObject(createSubscriber);
JObject jObject = JObject.Parse(json);
jObject["streetNumber"].Rename("number");
jObject["ZipCode"].Rename("zipcode");
json = jObject.ToString(/*Formatting.None*/);
}
catch (System.Exception ex)
{
throw ex;
}
See model:
var createSubscriber = new SubscriberRequest
{
Code = "cliente03",
Email = "[email protected]",
FullName = "Nome Sobrenome",
Cpf = "22222222222",
Phone_Area_Code = "11",
Phone_Number = "934343434",
BirthDate_Day = "26",
BirthDate_Month = "04",
BirthDate_Year = "1980",
Address = new Address
{
Street = "Rua Nome da Rua",
StreetNumber = "100",
Complement = "casa",
District = "Nome do Bairro",
City = "São Paulo",
State = "SP",
Country = "BRA",
ZipCode = "05015010"
},
Billing_Info = new Billing_Info
{
Credit_Card = new Credit_Card
{
Holder_Name = "Nome Completo",
Number = "4111111111111111",
Expiration_Month = "06",
Expiration_Year = "22"
}
}
};
See the class Address.Cs:
public class Address
{
[JsonProperty("streetNumber", DefaultValueHandling = DefaultValueHandling.Ignore)]
public string StreetNumber { get; set; }
[JsonProperty("street", DefaultValueHandling = DefaultValueHandling.Ignore)]
public string Street { get; set; }
[JsonProperty("city", DefaultValueHandling = DefaultValueHandling.Ignore)]
public string City { get; set; }
[JsonProperty("complement", DefaultValueHandling = DefaultValueHandling.Ignore)]
public string Complement { get; set; }
[JsonProperty("district", DefaultValueHandling = DefaultValueHandling.Ignore)]
public string District { get; set; }
[JsonProperty("zipCode", DefaultValueHandling = DefaultValueHandling.Ignore)]
public string ZipCode { get; set; }
[JsonProperty("state", DefaultValueHandling = DefaultValueHandling.Ignore)]
public string State { get; set; }
[JsonProperty("type", DefaultValueHandling = DefaultValueHandling.Ignore)]
public string Type { get; set; }
[JsonProperty("country", DefaultValueHandling = DefaultValueHandling.Ignore)]
public string Country { get; set; }
}
When trying to run this line jObject["streetNumber"].Rename("number");
, get that mistake:
Cannot Rename a null token Parameter name: token
The idea is: rename streetNumber
for number
before making JsonConvert.SerializeObject(json)
.
Some solution ?!?!
Thanks, solved my problem :D
– Matheus Miranda