简体   繁体   中英

deserializing json array into object into c# object

I'm doing an project detecting 3D coordinate of 32 bone(or joint) of human body in python and pass it to c# (because I need to simulate the pose in unity). I use json to pass the coordination data. below is the structure and json format.

public class Bone
{
    public string boneID; // the id can be 0~31
    public int[] coord = new int[3]; // means [x,y,z] 
}
public class Body
{
    public Bone[] bone = new Bone[32];
}

F:\code\python\project\AR_project\data.txt:

[{"boneID": 0, "coord": [0.5582984089851379, 0.4006982743740082, -0.8026577830314636]}, {"boneID": 1, "coord": [0.5646716356277466, 0.3453246057033539, -0.7448152303695679]}, {"boneID": 2, "coord": [0.5784791707992554, 0.34621402621269226, -0.7446972727775574]}, {"boneID": 3, "coord": [0.5923898816108704, 0.3476247191429138, -0.7444829940795898]}, {"boneID": 4, "coord": [0.51494300365448, 0.348619669675827, -0.7711067795753479]}, {"boneID": 5, "coord": [0.4918557107448578, 0.35328584909439087, -0.7705037593841553]}, {"boneID": 6, "coord": [0.46971043944358826, 0.35948294401168823, -0.7708505988121033]}, {"boneID": 7, "coord": [0.6031385064125061, 0.3890209496021271, -0.42907142639160156]}, {"boneID": 8, "coord": [0.4273480474948883, 0.41100919246673584, -0.5215374827384949]}, {"boneID": 9, "coord": [0.5876436233520508, 0.4774358570575714, -0.680288553237915]}, {"boneID": 10, "coord": [0.5307889580726624, 0.4794893264770508, -0.7098418474197388]}, {"boneID": 11, "coord": [0.7313540577888489, 0.744981050491333, -0.14570817351341248]}, {"boneID": 12, "coord": [0.2881341576576233, 0.7828477025032043, -0.5022284984588623]}, {"boneID": 13, "coord": [0.8250340223312378, 1.0933029651641846, -0.13745900988578796]}, {"boneID": 14, "coord": [0.21137109398841858, 1.2308467626571655, -0.6026167869567871]}, {"boneID": 15, "coord": [0.9296056628227234, 1.2383787631988525, -0.674834668636322]}, {"boneID": 16, "coord": [0.29519137740135193, 1.4980947971343994, -1.0327577590942383]}, {"boneID": 17, "coord": [0.9903305768966675, 1.2894338369369507, -0.8039193153381348]}, {"boneID": 18, "coord": [0.29626941680908203, 1.6103172302246094, -1.154832124710083]}, {"boneID": 19, "coord": [0.9499425888061523, 1.2687755823135376, -0.7644013166427612]}, {"boneID": 20, "coord": [0.3366844058036804, 1.578894853591919, -1.216864824295044]}, {"boneID": 21, "coord": [0.914238452911377, 1.2527515888214111, -0.7090128064155579]}, {"boneID": 22, "coord": [0.34219926595687866, 1.5337172746658325, -1.0733346939086914]}, {"boneID": 23, "coord": [0.638580858707428, 1.495591402053833, 0.05181015282869339]}, {"boneID": 24, "coord": [0.3468281626701355, 1.5188297033309937, -0.046819668263196945]}, {"boneID": 25, "coord": [0.643194854259491, 2.103864908218384, 0.17427441477775574]}, {"boneID": 26, "coord": [0.391088604927063, 2.1466524600982666, 0.023828966543078423]}, {"boneID": 27, "coord": [0.6543149948120117, 2.655918598175049, 0.8138496279716492]}, {"boneID": 28, "coord": [0.39941099286079407, 2.689655065536499, 0.4511289596557617]}, {"boneID": 29, "coord": [0.6524249315261841, 2.7533462047576904, 0.8432862162590027]}, {"boneID": 30, "coord": [0.3807646632194519, 2.786376714706421, 0.4773707389831543]}, {"boneID": 31, "coord": [0.6774906516075134, 2.820152521133423, 0.3530712425708771]}]

Now, to read the coordinate in C#, I use function in Newtonsoft.Json (visual studio), but it seems I can't find a correct way to use it wisely. ( the code below has error, but I think it can express what I want)

class Test
{
    public static void Main()
    {
        //Body bone;
        Body body;
        string path = @"F:\code\python\project\AR_project\data.txt";
        using (StreamReader r = new StreamReader(path))
        {
            string json = r.ReadToEnd();
            Console.WriteLine(json);
            body = JsonConvert.DeserializeObject <Body> (json);
            foreach (Bone bone in body)
            {
                Console.WriteLine("{0},{1},{2},{3}\n", bone.boneID, bone.coord[0], bone.coord[1], bone.coord[2]);
            }
        }
    }
}

I want my code allow me to print out the object attribue, which potentially means I had set the json value correctly into the object instance (body) ). I hope it print like below.

0, 0.5582984089851379, 0.4006982743740082, -0.8026577830314636
1, 0.5646716356277466, 0.3453246057033539, -0.7448152303695679
2, 0.5784791707992554, 0.34621402621269226, -0.7446972727775574
.
.
.
31, 0.6774906516075134, 2.820152521133423, 0.3530712425708771

I'm using visual studio 2022, have downloaded Newtonsoft.Json. If I make you feel I doesn't do my effort, I'm sorry about that, but I have been searching answer for 10 hours these three days. Even a suggestion is helpful. thank you.

Define coordinate as a double or decimal array

public class Bone
{
    // consider using properties instead of fields
    public string boneID;
    public double[] coord = new double[3]; // means [x,y,z] 
}

Also what you have in the file is an array of objects, not an object with bone field. Deserialize data as a collection of bones:

var bones = JsonConvert.DeserializeObject<List<Bone>>(json);
foreach (Bone bone in bones)
{
   // 
}

Use List<double> in coord

 public class Root
    {
       [JsonProperty("boneID")]
       public long BoneId { get; set; }

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

and deserialize using list

var result = JsonConvert.DeserializeObject<List<Root>>(json);
        
foreach(var datas in result){
    Console.WriteLine(datas.Coord[0]);
}   

check your example in this site

Define boneId as an int and coord as an array of double and deserialze the object into a bone rather than body


public class Bone
{
  public int boneID;
  public double[] coord;
}

 Bone bones;
 string path = @"F:\code\python\project\AR_project\data.txt";
 using (StreamReader r = new StreamReader(path))
  {
    string json = r.ReadToEnd();
     Console.WriteLine(json);
     bones = JsonConvert.DeserializeObject <Bone> (json);
 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM