-2

Could U help me Deserializing JSON to C#?
I just eat my teeth on it.. =.=.

I find many methods how to do not solve this xD,
I dont want share that with U.
I wish to wait for your suggestions.

Usefull links:
http://jsonviewer.stack.hu/
http://json2csharp.com/

JSON looks..

[
   {
      "faceId":"626f5974-1d63-40d4-98f1-7e6a7df13dba",
      "faceRectangle":{
         "top":108,
         "left":699,
         "width":208,
         "height":208
      },
      "faceAttributes":{
         "smile":0.973,
         "gender":"male",
         "age":25.7,
         "emotion":{
            "anger":0.0,
            "contempt":0.026,
            "disgust":0.0,
            "fear":0.0,
            "happiness":0.973,
            "neutral":0.001,
            "sadness":0.0,
            "surprise":0.0
         }
      }
   },
   {
      "faceId":"bc051f1d-9a64-4e86-bf95-2af1de21d316",
      "faceRectangle":{
         "top":104,
         "left":634,
         "width":114,
         "height":114
      },
      "faceAttributes":{
         "smile":0.074,
         "gender":"male",
         "age":17.4,
         "emotion":{
            "anger":0.003,
            "contempt":0.003,
            "disgust":0.001,
            "fear":0.002,
            "happiness":0.074,
            "neutral":0.828,
            "sadness":0.079,
            "surprise":0.01
         }
      }
   }
]
Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
  • 1
    What have you tried so far? What C# objects do you want to deserialize it into? You haven't given us much to go on. – Matt Hogan-Jones Jun 12 '17 at 10:36
  • 3
    *I dont want share that with U.* I'm afraid it doesn't work that way. You need to share what you have tried and tell us what is the problem with it. Then we will be able to help you. You should also go over those topics https://stackoverflow.com/help/asking – Guy Jun 12 '17 at 10:37
  • Cannot understand what do you want :))) – QuietNaN Jun 12 '17 at 10:37
  • Possible duplicate of [Deserialize JSON into C# dynamic object?](https://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object) – Pribina Jun 12 '17 at 10:38
  • Apologize for my lack of competence, but Sir Rufo understood me ;3 I said: Could U help me Deserializing JSON to C#? So He gessed, than I wish deserializing JSON to some smart class, and he show me how to get access to objects ;) - I appreciate. @Guy I said: I dont want share that with U cuz I was ashamed for my scribble.. Thank U for welcome in stacksociety ;3 #Cold_shower – Szymon Tobolewski Jun 12 '17 at 12:32

8 Answers8

1

First of all you need classes to which you want to deserialize, so you can create manually or the fastest and easiest way - you can copy json string that you have, then in visual studio

Edit -> Paste Special -> Paste JSON as Classes

Now you have structure for deserializing.Then you can for example use Newtonsoft.Json (which you can download from NuGet). There is JsonConvert.DeserializeObject<T>() generic method, that will do all deserialization work for you.

Also, if you want use your own property names in the class structure, you can use [JsonProperty("Name")] attribute, which will change the names of properties when they are serialized to JSON and vice versa.

SᴇM
  • 7,024
  • 3
  • 24
  • 41
0

Do you mean something like this? To deserialize to an object?

using System.Web.Script.Serialization;

public class FaceRectangle
{
    public int top { get; set; }
    public int left { get; set; }
    public int width { get; set; }
    public int height { get; set; }
}

public class Emotion
{
    public double anger { get; set; }
    public double contempt { get; set; }
    public double disgust { get; set; }
    public double fear { get; set; }
    public double happiness { get; set; }
    public double neutral { get; set; }
    public double sadness { get; set; }
    public double surprise { get; set; }
}

public class FaceAttributes
{
    public double smile { get; set; }
    public string gender { get; set; }
    public double age { get; set; }
    public Emotion emotion { get; set; }
}

public class RootObject
{
    public string faceId { get; set; }
    public FaceRectangle faceRectangle { get; set; }
    public FaceAttributes faceAttributes { get; set; }
}

return JsonConvert.DeserializeObject<RootObject>(jsonString);
Jon B
  • 11
  • 2
0

If you want c# object from that JSON, this is how it is done:

public class FaceRectangle
{
    public int top { get; set; }
    public int left { get; set; }
    public int width { get; set; }
    public int height { get; set; }
}

public class Emotion
{
    public double anger { get; set; }
    public double contempt { get; set; }
    public double disgust { get; set; }
    public double fear { get; set; }
    public double happiness { get; set; }
    public double neutral { get; set; }
    public double sadness { get; set; }
    public double surprise { get; set; }
}

public class FaceAttributes
{
    public double smile { get; set; }
    public string gender { get; set; }
    public double age { get; set; }
    public Emotion emotion { get; set; }
}

public class RootObject
{
    public string faceId { get; set; }
    public FaceRectangle faceRectangle { get; set; }
    public FaceAttributes faceAttributes { get; set; }
}

var obj = JsonConvert.DeserializeObject<RootObject>(json);
QuietNaN
  • 371
  • 1
  • 14
0

You can convert Json to c# Class

public class FaceRectangle  
{

    public int top { get; set; }
    public int left { get; set; }
    public int width { get; set; }
    public int height { get; set; }
}

public class Emotion
{
    public double anger { get; set; }
    public double contempt { get; set; }
    public double disgust { get; set; }
    public double fear { get; set; }
    public double happiness { get; set; }
    public double neutral { get; set; }
    public double sadness { get; set; }
    public double surprise { get; set; }
}

public class FaceAttributes
{
    public double smile { get; set; }
    public string gender { get; set; }
    public double age { get; set; }
    public Emotion emotion { get; set; }
}

public class Example
{
    public string faceId { get; set; }
    public FaceRectangle faceRectangle { get; set; }
    public FaceAttributes faceAttributes { get; set; }
}

Example Example_class = 
Newtonsoft.Json.JsonConvert.DeserializeObject<Example>(json.ToString());

Please see this useful link Json to C# Class https://jsonutils.com/

0

Your JSON document represents an array so you must deserialize it to a collection

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

public class Program
{
    public static void Main()
    {
        string jsonString = "[ {\"faceId\": \"626f5974-1d63-40d4-98f1-7e6a7df13dba\",\"faceRectangle\": {  \"top\": 108,  \"left\": 699,  \"width\": 208,  \"height\": 208     },\"faceAttributes\": {  \"smile\": 0.973,  \"gender\": \"male\",  \"age\": 25.7,  \"emotion\": {    \"anger\": 0.0,    \"contempt\": 0.026,    \"disgust\": 0.0,    \"fear\": 0.0,    \"happiness\": 0.973,    \"neutral\": 0.001,    \"sadness\": 0.0,    \"surprise\": 0.0        }    }},{\"faceId\": \"bc051f1d-9a64-4e86-bf95-2af1de21d316\",\"faceRectangle\": {  \"top\": 104,  \"left\": 634,  \"width\": 114,  \"height\": 114     },\"faceAttributes\": {  \"smile\": 0.074,  \"gender\": \"male\",  \"age\": 17.4,  \"emotion\": {    \"anger\": 0.003,    \"contempt\": 0.003,    \"disgust\": 0.001,    \"fear\": 0.002,    \"happiness\": 0.074,    \"neutral\": 0.828,    \"sadness\": 0.079,    \"surprise\": 0.01        }    }}]";
        var result = JsonConvert.DeserializeObject<IList<RootObject>>(jsonString);

        foreach ( var item in result )
            Console.WriteLine( item.faceId );
    }
}

// Generated by http://json2csharp.com

public class FaceRectangle
{
    public int top { get; set; }
    public int left { get; set; }
    public int width { get; set; }
    public int height { get; set; }
}

public class Emotion
{
    public double anger { get; set; }
    public double contempt { get; set; }
    public double disgust { get; set; }
    public double fear { get; set; }
    public double happiness { get; set; }
    public double neutral { get; set; }
    public double sadness { get; set; }
    public double surprise { get; set; }
}

public class FaceAttributes
{
    public double smile { get; set; }
    public string gender { get; set; }
    public double age { get; set; }
    public Emotion emotion { get; set; }
}

public class RootObject
{
    public string faceId { get; set; }
    public FaceRectangle faceRectangle { get; set; }
    public FaceAttributes faceAttributes { get; set; }
}

.net fiddle

Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
  • How fast U did this... How fast U solve this... -.- Cuz U get lunch brake, so U check stackoverflow with sandwich in hand and U make this xd... Thanks Mate. Before: I try get only to faceRectangle like to some array.., i replace some characters { => [ etc. Something works, but i get trouble with get there -.- (no sens) so the IList solve problem ;) thx again – Szymon Tobolewski Jun 12 '17 at 11:27
0

Instead of giving you a C# class that maps to the JSON which you'll just copy blindly, I'll recommend that you generate such a class yourself.

  1. Make sure you're running Visual Studio 2013 SP2 or higher.
  2. Get a JSON document for your data. If it has optional fields, make sure the one you have is as full as possible.
  3. In a new CS file in VS, choose Edit -> Paste Special -> Paste JSON as Classes.

This will create a C# class that corresponds to the JSON data. Now, you can use JSON.NET's JsonConvert.DeserializeObject() to create it

Avner Shahar-Kashtan
  • 14,492
  • 3
  • 37
  • 63
0

By using NewtonsoftJson library to parse data easily

Example

dynamic x = Newtonsoft.Json.JsonConvert.DeserializeObject(Jsondata); //to parse data
foreach (var product in x) { 
     Messagebox.show(product.data.ToString());
   }
Znaneswar
  • 3,329
  • 2
  • 16
  • 24
0

I hope this will work

string Jsondata = "Your Json data";

public class Mainclass
{
   public guid faceId;
   IEnumerable<faceRectangle>
   IEnumerable<faceAttributes>
}
public class faceRectangle
{

}

public class faceAttributes
{

}


Mainclass backdata =  JsonConvert.DeserializeObject<Mainclass>(Jsondata , new DataConverter())
Parth Mehta
  • 87
  • 1
  • 7