Jump to content

C# - Weird JSON "array" but not really array handling

Go to solution Solved by Aleksa Djordjic,

Well, turns out I can just deserialize it in to a dynamic, and then access that "array" but not really an array object and then just iterate trough it.

That makes it iterate trough each property of that object, casting it to JProperty I can then just do .Value["Property name"] to get its value of that specific property inside that array element

I'm working with an API that has a weird way of doing arrays which I just can't understand how to convert to a C# array/list.

Note: I'm using Newtonsoft.Json

 

The JSON "array" looks something like this:

Values : 
{
	"0": { "some data" },
	"1": { "more data" },
	"2": { "even more data" }
}

The thing is that the array itself isn't actually an array, its an object containing objects which have their name as array index and value their data... I don't get why the API was made this way, how you would handle this even in JavaScript

 

For smaller arrays like this I did something like this, using JsonProperty in a way that I know:

class Data 
{
  public string Value { get; set; }
}

[JsonProperty("0")]
public Data Zero { get; set; }

[JsonProperty("1")]
public Data One { get; set; }

[JsonProperty("2")]
public Data Two { get; set; }

public Data[] All 
{
 get
 {
   return new Data[] 
   {
     Zero, One, Two
   }
 }
}

It works... kind of a hacky way of doing it, but some arrays are up to couple of thousand elements big and I have no clue if they are fixed or can expand in the future so this way of doing it is not practical at all.

 

I have no control over the API, I can just fetch that data

 

I have tried searching around if maybe there is a wildcard for the JsonPropertyAttribute name so that it can handle this kind of stuff but haven't found anything helpful or anyone else working with JSON of this type.

Has anyone worked with something like this before and/or has an idea of how to handle this?

 

Thank you in advance!

Link to comment
Share on other sites

Link to post
Share on other sites

I have absolutely no experience with JSON files, APIs or even C#. But, I have a little experience with objects and arrays and I can try to do this in Java. My understanding is that Java and C# are kinda similar so I hope you can convert my code into C#. 

 

My understanding of your issue is that the API is returning an object to you. In your case, its returning an object called Values. 'Values' contains more objects called '0', '1', '2' etc which have the data inside. the name of the object corresponds to the order in the 'array' they are intended to be in. 

 

What you want to do is take the Values object and convert the entries inside it all into a single array in C#.

 

 

Here is my attempt at this in Java:

//json array is called APIValues

Data values[] = new Data[size];
for(int i = 0; i < size; i++){
	values[i] = APIValues.id(i);
}

What i'm trying to do here, it to make a loop that takes the object at the JSON index of 'i' and puts it into an array at index 'i'. 

 

 

Again, i have no idea how to use JSON and I don't know the C# language, but i'm hoping you can infer what to do here. 

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, Saksham said:

I have absolutely no experience with JSON files, APIs or even C#. But, I have a little experience with objects and arrays and I can try to do this in Java. My understanding is that Java and C# are kinda similar so I hope you can convert my code into C#. 

 

My understanding of your issue is that the API is returning an object to you. In your case, its returning an object called Values. 'Values' contains more objects called '0', '1', '2' etc which have the data inside. the name of the object corresponds to the order in the 'array' they are intended to be in. 

 

What you want to do is take the Values object and convert the entries inside it all into a single array in C#.

 

 

Here is my attempt at this in Java:


//json array is called APIValues

Data values[] = new Data[size];
for(int i = 0; i < size; i++){
	values[i] = APIValues.id(i);
}

What i'm trying to do here, it to make a loop that takes the object at the JSON index of 'i' and puts it into an array at index 'i'. 

 

 

Again, i have no idea how to use JSON and I don't know the C# language, but i'm hoping you can infer what to do here. 

Hmmm, ok that might work in Java, and yeah it is similar to C# quite a bit.
Although I doubt that would work.

For me to deserialize a JSON object with Newtonsoft.Json i have to have a premade object with the data that I want to deserialize.

 

Although you just gave me an idea that I can try, deseriazlizing it into a dynamic will just deserialize everything, then with reflection I can just get how many properties the object has, and then loop trough that many times getting the parameter with again, reflection, by using its index as the parameter name.

Never done reflection before, and I'm not sure if its called that, I think it is but a good opportunity to learn it.

 

Thanks either way, although would be nice if it was a quick solution that I could implement right now

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Aleksa Djordjic said:

For me to deserialize a JSON object with Newtonsoft.Json i have to have a premade object with the data that I want to deserialize.

 

Although you just gave me an idea that I can try, deseriazlizing it into a dynamic will just deserialize everything, then with reflection I can just get how many properties the object has, and then loop trough that many times getting the parameter with again, reflection, by using its index as the parameter name.

Never done reflection before, and I'm not sure if its called that, I think it is but a good opportunity to learn it.

 

Thanks either way, although would be nice if it was a quick solution that I could implement right now

no idea what any of that means but glad to help even a little. 

Link to comment
Share on other sites

Link to post
Share on other sites

Well, turns out I can just deserialize it in to a dynamic, and then access that "array" but not really an array object and then just iterate trough it.

That makes it iterate trough each property of that object, casting it to JProperty I can then just do .Value["Property name"] to get its value of that specific property inside that array element

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×