Reputation: 77
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
Upvotes: 1
Views: 136
Reputation: 1248
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"]
Upvotes: 5