0

Firstly thank you for taking the time to look at this. It's quite alot. Question: I'm basically trying to download a json as a string and then deserialize it to a list. The reason why is so i can then call a specific property of that list (in my case 'ips' because it's all i actually need) and insert it into a table if requirements are met. The problem is that it moves all null values into the array. 114 columns of null, or empty array and i can't figure out why? I think i'll attach a link to the JSON because its a massive file its here https://endpoints.office.com/endpoints/Worldwide?clientRequestId=b10c5ed1-bad1-445f-b386-b919946339a7

Here is my code:

  1. Getters and setters for JSON
       public class GetSetJsonIP {
       [JsonProperty("id")]
       public int id { get; set; }
    
       [JsonProperty("serviceArea")]
       public string ServiceArea { get; set; }
    
       [JsonProperty("serviceAreaDisplayName")]
       public string ServiceAreaDisplayName { get; set; }
    
       [JsonProperty("urls")]
       public IList<string> urls { get; set; }
    
       [JsonProperty("ips")]
       public IList<string> ips { get; set; }
    
       [JsonProperty("tcpPorts")]
       public string tcpPorts { get; set; }
    
       [JsonProperty("expressRoute")]
       public bool expressRoute { get; set; }
    
       [JsonProperty("category")]
       public string category { get; set; }
    
       [JsonProperty("required")]
       public bool required { get; set; }
    
       [JsonProperty("notes")]
       public string notes { get; set; }
    
       [JsonProperty("udpPorts")]
       public string udpPorts { get; set; }
       }
    
    
  2. List class
    public class ConvertJsonIP{
    public List<GetSetJsonIP> jsonIpConvert { get; set; }
    public List<GetSetJsonIP> jsonIPConvert = new List<GetSetJsonIP>();
    }

3.I download the JSON using an empty string called o365IP

o365IP = wc.DownloadString(wc.BaseAddress + "/endpoints/Worldwide?clientRequestId=b10c5ed1-bad1-445f-b386-b919946339a7");
  1. I deserialize using my List to a seperate var
var o365IpVerion = JsonConvert.DeserializeObject<List<ConvertJsonIP>>(o365IP);

This code shows no errors. so i can only assume its a logical one on my part. It should be noted that i had to put the <List< in to stop an error stating that it couldnt convert an object to an array.

Seriously, i've been stuck on this for 3 days so any help on this would be greatly appreciated! Thanks in advance!

4
  • If anyone can't get to the link, i'll post the whole JSON. Just didn't want to blind anyone going through about 600 lines of JSON.. Also if anyone knows a way to only take a part of a JSON e.g. in my case i only want the values for id: 1 including the url's and ip's i'll be really greatful Commented Aug 25, 2020 at 22:59
  • 1
    Whats the name of the class for number 1. Commented Aug 25, 2020 at 23:06
  • @Jawad Sorry must have missed it.. The name is 'GetSetJsonIP' Commented Aug 25, 2020 at 23:09
  • @Jawad and the name for class 2 is 'ConvertJsonIP' Commented Aug 25, 2020 at 23:13

2 Answers 2

2

the json you have is a list of objects and each of these objects conform to GetSetJsonIp. You should deserialize using List<GetSetJsonIP>

var o365IpVerion = JsonConvert.DeserializeObject<List<GetSetJsonIP>>(o365IP);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you SO MUCH! honestly i cannot say it enough. 3 day's i've been stuck on this and all this time it was that! Once again, thank you. I think it's that thing where you look at the code so much your brain just goes numb to it
1

public class GetJsonIP works fine.

The reason you must Deserialize into a List<> is because the json object starts with a bracket making the entire object a List or array.

var O365IpVersion = JsonConvert.DeserializeObject<List<GetJsonIP>(O365IP);

There are different ways to fetch the value of a certain property. If you just need ips and want to check the value then update it, then you could loop:

JArray arr = JArray.Parse(O365IP);
foreach (JObject obj in arr.Children<JObject>())
{
    foreach (JPRoperty prop in obj.Properties().Where(x => x.Name == "ips"))
    {
         //use prop.Value and perform tasks
    }
}

Or just simply loop like this:

for (int i = 0; i < O365IpVersion.Count; i++)
{
    //use O365IpVersion.ElementAt(i).ips

2 Comments

Hi. I've got to say i never even thought to do this. Im currently altering my code because this has destroyed my efficiency 10 fold. Thank you so much! I'm an intern right now so new to stack overflow otherwise i would have upvoted this!
Thank you, but do you mean this code that I posted 'destroyed efficiency 10 fold?` What exactly is destroying efficiency?

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.