2017-06-12 8 views
-2

JSONをC#にデシリアライズするのに役立つでしょうか?
私はちょうどそれに私の歯を食べる.. =。=。

私はこのxDを解決しない方法を見つけました。
私はそれをUと共有したくありません。
私はあなたの提案を待っています。

お役立ちリンク:
http://jsonviewer.stack.hu/
http://json2csharp.com/

JSONに見える.. JSONデータをC#にデシリアライズする

[ 
    { 
     "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 
     } 
     } 
    } 
] 
+1

これまでに何を試しましたか?どのC#オブジェクトをデシリアライズしますか?あなたは私たちに多くを与えていない。 –

+3

*私はUとそれを共有したくない。*私はそれがうまくいかないと思う。あなたが試したことを分かち合い、問題の内容を教えてください。その後、私たちはあなたを助けることができるでしょう。また、[C#の動的なオブジェクトに逆シリアル化JSON?])) – Guy

+0

をhttps://stackoverflow.com/help/askingこれらのトピックは、あなたが:)何をしたいですか理解できないの – QuietNaN

答えて

0

JSONドキュメントはarrayを表しますので、deseriaコレクションの代わりにあなたがやみくもにコピーしますJSONにマップC#クラスを与える

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

+0

例外がスローされます... -.- CuzのUはランチブレーキを取得するので、Uはサンドイッチとstackoverflowのチェック手にして、Uはこのxdを作る... ありがとう。そう ..私はいくつかの配列に似faceRectangleにのみ取得してみてください 、私はいくつかの文字{=>を置き換える[など 何かが動作しますが、私は(何SENS)をそこに着くとのトラブルを取得していない-.-: 前IList解決問題;) thx再び –

0

あなたはこのような何かを意味していますか?オブジェクトにデシリアライズするには?

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); 
+0

あなたのコードが返されますJSONからの二つのオブジェクトのどちらを? –

0

あなたはそのJSONからC#のオブジェクトをしたい場合は、これはそれがどのように行われるかである。

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); 
+0

このUは、この問題を解決するどのくらいの速... Uはこれをしなかったどのくらいの速=> https://dotnetfiddle.net/3e4Wzd –

0

あなたはC#クラスに

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()); 

をJSONに変換することができ、この便利なリンクJSONを参照してください。 C#クラスhttps://jsonutils.com/

0

にそれを平安大町、私はあなたがそのようなクラスを自分で生成することをお勧めします。

  1. Visual Studio 2013 SP2以上を実行していることを確認してください。
  2. データ用のJSONドキュメントを取得します。オプションのフィールドがある場合は、フィールドができるだけいっぱいであることを確認してください。
  3. VSの新しいCSファイルで、[編集] - > [形式を選択して貼り付け] - > [JSONをクラスとして貼り付け]を選択します。

これは、JSONデータに対応するC#クラスを作成します。あなたがコピーすることができます - 今、あなたは

1

あなたが手動で作成することができますので、あなたは、あなたが非直列化するためのクラスを必要とするすべて、または最も簡単な方法の最初にそれを作成するためにJSON.NETのJsonConvert.DeserializeObjectを()を使用することができます>スペシャル貼り付け - - クラス

として>貼り付けJSONを今、あなたはあなたが例えばを使用することができますdeserializing.Thenのための構造を持っているあなたは、[編集]

Visual StudioでいることをJSON文字列(NuGetからダウンロードできます)。 JsonConvert.DeserializeObject<T>()ジェネリックメソッドがあります。これはすべてのデシリアライゼーション作業を行います。

class構造内に独自のプロパティ名を使用する場合は、[JsonProperty("Name")]属性を使用すると、プロパティの名前がJSONにシリアル化されるときに変更され、その逆もあります。

+0

うわー。 Thx、よく知る;) 毎日私はより良い;) よろしくご協力ありがとうございます –

+0

よろしくお願いします! – SeM

0

簡単にデータを解析するNewtonsoftJsonライブラリを使用することにより

dynamic x = Newtonsoft.Json.JsonConvert.DeserializeObject(Jsondata); //to parse data 
foreach (var product in x) { 
    Messagebox.show(product.data.ToString()); 
    } 
0

私はこれが

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())