-1

I want to parse json into an object, problem is I don't have the key names in the json, only the values. Is it possible to parse these values into an object?

Code example:

public class Test
{
    public uint one;
    public bool trueOrFalse;
    public uint three;
}

private void ParseTest()
{
    Test test = JsonConvert.DeserializeObject<Test>("[1, true, 3]");
}

Error I get using Newtonsoft.Json:

JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,true,3]) into type 'Test' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

Edit: What I want is to unfold the array onto an object's properties, in declaration order.

I work with a project where I get a lot of these value arrays and I cannot write a custom convert function for all of them. My first solution was a function that would insert the field names into the json before parsing, but this eventually broke when the json structure became more complicated (class in a class...).

dbc
  • 104,963
  • 20
  • 228
  • 340
Daniel
  • 11
  • 1
  • 2
    The type you deserialize into must match your JSON exactly. Your JSON is a number array, so `JsonConvert.DeserializeObject(...)` would seem like a good fit. – CodeCaster Sep 08 '22 at 15:14
  • That would work for this example, but lets say I have a struct with different types of values? Like this: public class Test { public uint one; public bool trueOrFalse; public uint three; } [1, true, 3] – Daniel Sep 08 '22 at 15:16
  • 5
    Then parse it to a `JArray` and extract the values from that. – Jon Skeet Sep 08 '22 at 15:17
  • 1
    Then you don't want to deserialize, you want to parse and inspect the type of each token: https://www.newtonsoft.com/json/help/html/ParseJsonArray.htm – CodeCaster Sep 08 '22 at 15:17
  • 2
    Then it's `object[]`. Also stop pasting that type, your json isn't an object, it's an array. – Blindy Sep 08 '22 at 15:18
  • Is it possible to extract the values from an JArray into a class/struct in a generic way? – Daniel Sep 08 '22 at 15:22
  • 1
    What do you mean by "generic"? If your JSON has no structure, i.e. everything is thrown into one array, you cannot model a class to it. Do you mean you want to unfold the array onto an object's properties, in declaration order? AFAIK you can't out of the box. It is also a horrific data structure if this is what you have to process. – CodeCaster Sep 08 '22 at 15:24
  • "Do you mean you want to unfold the array onto an object's properties, in declaration order?" Yes this is exactly what I want, I work with a project where I get a lot of these value arrays and I cannot write a custom convert function for all of them. My first solution was a function that would insert the field names into the json before parsing, but this eventually broke when the json structure became more complicated (class in a class...). Do you have an idea how to achieve this? – Daniel Sep 08 '22 at 15:28
  • @Daniel indeed you can't write converter of any kind - you need source code parser to achieve what you want as there is no way to get list of properties/fields in declaration order (https://stackoverflow.com/questions/14734374/c-sharp-reflection-property-order). – Alexei Levenkov Sep 08 '22 at 15:34
  • @Daniel Note that you really should [edit] in your last comment into the question. – Alexei Levenkov Sep 08 '22 at 15:34
  • 1
    *Is it possible to extract the values from an JArray into a class/struct in a generic way?* -- if that is what you need then [How to deserialize an array of values with a fixed schema to a strongly typed data class?](https://stackoverflow.com/q/39461518/3744182) is probably what you want. You mark the properties of the class to be deserialized with order attributes (e.g. `[JsonProperty(Order = 1)]`) then apply `[JsonConverter(typeof(ObjectToArrayConverter))]` to it. – dbc Sep 08 '22 at 15:39
  • Does this answer your question? [Deserialize JSON into C# dynamic object?](https://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object) – Ibrennan208 Sep 08 '22 at 15:41
  • @Ibrennan208 no, "dynamic" is a buzzword that's abused too much and will be of no help here. – CodeCaster Sep 08 '22 at 15:45
  • @dbc nice answer there, think that should solve OP's problem. – CodeCaster Sep 08 '22 at 15:46
  • @dbc Thanks! this is pretty promising, I will try it out. If you submit it as an answer I can accept it. – Daniel Sep 08 '22 at 15:50
  • @Daniel - rather than submitting it as a duplicate answer, I'd propose to mark the question as duplicate, unless you have some additional requirement not covered by that question. Is that OK? – dbc Sep 08 '22 at 15:52

1 Answers1

-1

your json is array, but you are trying to deserialize object. This code will work for you

var json="[1, true, 3]";
var jsonArray=JArray.Parse(json.Dump()).ToArray();

or if you want to convert to c# class

    Test test = new Test
    {
        one= (uint)jsonArray[0],
        trueOrFalse= (bool)jsonArray[1]
        three= (uint)jsonArray[2],
    }

this json will work for your class

var json = "{\"one\":1, \"trueOrFalse\":true, \"three\": 3}";
Test test = JsonConvert.DeserializeObject<Test>(json);
Serge
  • 40,935
  • 4
  • 18
  • 45
  • Thanks! But in my case I have several classes with different types of variables, in which case this solution wont work. I edited the question to hopefully make it clearer. – Daniel Sep 08 '22 at 15:41
  • @Daniel I edited my answer too. If you have serveral different jsons you have to include all of them in your post and tell as what data do you need. – Serge Sep 08 '22 at 15:43
  • Sorry but this doesn't work for me as I cannot alter the json. Also sadly because there are a lot these instances in my project, converting it manually isn't an option and I need a generic solution. – Daniel Sep 08 '22 at 15:57
  • @Daniel I see that the main problem is that you still don't know what you really need and can't explain it to us. – Serge Sep 08 '22 at 16:08
  • If you read my question you see exactly what I want: What I want is to unfold the array onto an object's properties, in declaration order. – Daniel Sep 08 '22 at 16:35
  • @Daniel Yes, this is what you want, but what you really need is completely different. – Serge Sep 08 '22 at 16:38