1

I'm going to parse the JSON data to class using C# and Newtonsoft.Json. But the JSON data is a little bit different than usual. Here is the JSON data sample:

{
    "ip_addresses": [
        "192.168.1.1"
     ],
    "ptr": {
        "192.168.1.1": "www.example.com"
    }
}

So the problem is the IP in ptr changes due to the IP in ip_addresses. I've successfully parsed ip_addresses data into the List. But I don't know how to do next with the List stored IP addresses.

class Server
{
    public string hostname { get; set; }
    public List<string> ip_addresses { get; set; }
    public override string ToString() 
    {
        string ip_set = string.Empty;
        foreach (string ip in ip_addresses)
        {
            ip_set += string.Format("{0} ", ip);
        }
        return string.Format("{0} {1}\n", hostname, ip_set)
    }
}
class Program
{
    static void Main(string[] args)
    {
        //json data is from the web response
        string responseContent = reader.ReadToEnd();
        Server server = JsonConvert.DeserializeObject<Server>(responseContent);

        Console.WriteLine(server);
        Console.ReadKey();
    }
}

Thanks a lot :D

1

1 Answer 1

5

Guessing the ptr is not an array.
Then the following property might work

public Dictionary<string,string> ptr {get; set;} 

If it is an array, then make it List<Dictionary<string,string>>

I'm on mobile, haven't checked it. Please verify

Update : forgot to add
Access it using ptr["ipAddress"]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.