0

I am trying to get the modhash value from a returned json string, I have set my 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:

        /
4334738290
  • 393
  • 2
  • 19
  • `int modhashPosition = responseString.IndexOf("modhash"); int commaPosition = responseString.IndexOf(",", modhashPosition); string result = responseString.Substring(modhashPosition + 11, commaPosition - modhashPosition - 12).Trim();` :D Just kidding, please don't... – Yeldar Kurmangaliyev May 10 '17 at 03:34
  • Being serious, you can a library JSON.NET to parse your string and access this property. There are really many similar questions and answers at SO about parsing JSON. For example, this: http://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c – Yeldar Kurmangaliyev May 10 '17 at 03:36
  • 1
    I noticed you're trying to set `mod_hash` by setting `modhash`. Try renaming your property to `modhash` without the underscore, or try setting `mod_hash` with underscore in the javascript? – WhiteRuski May 10 '17 at 04:16

1 Answers1

2

Try with the below one.

To deserialize,you need to create the proper class structure for the json string. As per your json string, i have created here. 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.

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

enter image description here

Hope it solves your problem.

Chandan Kumar
  • 4,570
  • 4
  • 42
  • 62