简体   繁体   English

从httpclient发布请求获取json值

[英]Getting json value from httpclient post request

I am trying to get the modhash value from a returned json string, I have set my getter/setter 我正在尝试从返回的json字符串中获取modhash值,我已经设置了我的getter / setter

public string mod_hash { get; set; } 

I am using httclient , how can I get the json value of mod_hash To post data: 我正在使用httclient ,如何获取mod_hash的json值以发布数据:

        /

Try with the below one. 尝试以下方法。

To deserialize,you need to create the proper class structure for the json string. 要反序列化,您需要为json字符串创建正确的类结构。 As per your json string, i have created here. 根据您的json字符串,我已经在这里创建了。 Try and let us know if you have still issues. 请尝试让我们知道您是否还有问题。

public class RootObject
{
    public Json json { get; set; }
}
public class Json
{
    public List<object> errors { get; set; }
    public Data data { get; set; }
}
public class Data
{
    public bool need_https { get; set; }
    public string modhash { get; set; }
    public string cookie { get; set; }
}

And to test if it is correct or not here i have the program to get the "modhash" property value from your json string. 并在这里测试是否正确,我有程序从您的json字符串中获取“ modhash”属性值。

class Program
{
    static void Main(string[] args)
    {
        string jsonstring = @"{ ""json"": {""errors"": [],""data"": { ""need_https"": true, ""modhash"": ""valuehereremoved"",""cookie"": ""valuehereremoved"" } } }";
        var serializer = new JavaScriptSerializer();
        var jsonObject = serializer.Deserialize<RootObject>(jsonstring);
        Console.WriteLine("modhash : " + jsonObject.json.data.modhash);
        Console.Read();
    }
}

OUTPUT 输出值

在此处输入图片说明

Hope it solves your problem. 希望它能解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM