{
"user": {
"id": 121,
"username": "luckygirl3",
"counter_rechecking": 0,
"user_id": 76,
"f_Id": "4334"
}
}
How to convert it to object and post it to api. I have already know how to post but I need to post user
object with parameters. I tried this:
JObject jobjects = new JObject();
JObject jobjectss = new JObject();
jobjects["user"] = jobjectss;
jobjectss["id"] = 121;
jobjectss["username"] = "luckygirlx3";
jobjectss["counter_rechecking"] = 0;
jobjectss["user_id"] = 76;
jobjectss["f_Id"] = "4334";
How to Convert Json to object c# To Post Data APi
This seems like you want to convert a JSON response to a object. If this is the case then you want to deserialize your JSON. You havn't posted your Post Data code, so I can't help much with that.
I would first map your JSON to classes:
public class User
{
public int Id { get; set; }
public string Username { get; set; }
[JsonProperty("counter_rechecking")]
public int CounterRechecking { get; set; }
[JsonProperty("user_id")]
public int UserId { get; set; }
[JsonProperty("f_id")]
public string FId { get; set; }
}
public class RootObject
{
public User User { get; set; }
}
Then deserialize your JSON to RootObject
with Json.NET :
var deserializedJson = JsonConvert.DeserializeObject<RootObject>(json);
You can then access your properties like this:
Console.WriteLine(deserializedJson.User.Id);
Console.WriteLine(deserializedJson.User.Username);
Console.WriteLine(deserializedJson.User.CounterRechecking);
Console.WriteLine(deserializedJson.User.UserId);
Console.WriteLine(deserializedJson.User.FId);
I used JsonProperty
to map your attributes with _
to nicer property members. This is optional however.
You can also use something like json2csharp.com or Edit -> Paste Sepcial -> Paste JSON as classes inside Visual Studio after copying your JSON to the clipboard to generate the classes for you.
Full demo at dotnetfiddle.net .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.