-1

I have a Json:

    [
    [
        [
            {
                "origin": [
                    -15.2941064136735,
                    -0.43948581648487,
                    4.83674058264479
                ],
                "dimensions": [
                    10.4597624323399,
                    11.6903227184975,
                    9.67348116528958
                ],
                "primitive": "block"
            }
        ],
        [
            {
                "origin": [
                    -15.2941064136735,
                    -0.43948581648487,
                    4.83674058264479
                ],
                "dimensions": [
                    10.4597624323399,
                    11.6903227184975,
                    9.67348116528958
                ],
                "primitive": "block"
            }
        ],
        [
            {
                "origin": [
                    -15.2941064136735,
                    -0.43948581648487,
                    4.83674058264479
                ],
                "dimensions": [
                    10.4597624323399,
                    11.6903227184975,
                    9.67348116528958
                ],
                "primitive": "block"
            }
        ]
    ]
]

And model for this Json:

public class BoxConverter
    {
        [JsonProperty("origin")]
        public List<double> Origin { get; set; }

        [JsonProperty("dimensions")]
        public List<double> Dimensions { get; set; }

        [JsonProperty("primitive")]
        public string Primitive { get; set; }
    }

And i try get list of objects from json which in file.

 string strLocal = File.ReadAllText("2.txt");
 var convertLocal = JsonConvert.DeserializeObject<List<BoxConverter>>(strLocal);

But I have a exception :

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

To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.

Path '[0]', line 1, position 2.

What I'm doing wrong?

UPDATE

With this JSON work perfectly :

[{"attributes":{"materialProperties":{"color":"red","wireframe":false}},"dimensions":[10.4597624323399,11.6903227184975,9.67348116528958],"origin":[-15.2941064136735,-0.43948581648487,4.83674058264479],"primitive":"block"}] 

1 Answers1

0

Your JSON is a list of a list of a list of BoxConverter.

You need to do this:

var convertLocal = JsonConvert.DeserializeObject<List<List<List<BoxConverter>>>>(strLocal)

Your BoxConverter class is fine.

If your json is nested an unknown amount of times (you should probably address why this is in the first place), you can do this:

var superHack = JsonConvert.DeserializeObject<dynamic>(json);
var currentObject = superHack;
while(currentObject is JArray)
    currentObject = currentObject[0];

var foundObject = currentObject as JObject;
var convertLocal = foundObject.ToObject<BoxConverter>();
Rob
  • 26,989
  • 16
  • 82
  • 98
  • But how check count of this lists inside Json. Because file can contains different Json whit different count of lists. – SomeGenericNameto_Display Nov 17 '15 at 12:11
  • @VolodymyrGnatiuk See edit – Rob Nov 17 '15 at 12:17
  • var foundObject = currentObject as JObject; Object reference not set to an instance of an object. foundObject=null; – SomeGenericNameto_Display Nov 17 '15 at 12:57
  • @VolodymyrGnatiuk Then the json you're testing with is not a list of (nested lists) of objects. You should really try to standardize your json. In any case, I can't help you without seeing the json you're testing on. I tested the above on both snippets, and it works. Nevertheless, you should be able to tweak it slightly to fit your new json. – Rob Nov 17 '15 at 12:59
  • I also try the both fragments and your code not working with first fragment only. Maybe problem in reading from file? I don't now why, but I can't get each of element in loop. And construction currentObject as Newtonsoft.Json.Linq.JObject; return null. – SomeGenericNameto_Display Nov 17 '15 at 13:32
  • Your sample working but I can get only first element . – SomeGenericNameto_Display Nov 17 '15 at 15:16