2

次のようなJSON文字列を取得しようとすると問題が発生します

[{Key:{key:value、key:value、key:value、key:value}、key:[{key:value、key:value、key:value、key:value}]

C#で。

私のクラス構造はこんな感じです

        public class wideeye
    {
        public listing listing { get; set; }
        public listing_images[] listing_images { get; set; }
    }

    public class listing
    {

        public string category { get; set; }
        public string city { get; set; }
        public string country { get; set; }
        public string created_at { get; set; }
        public string current_publish_date { get; set; }
        public string details { get; set; }
        public string id { get; set; }
        public string industry { get; set; }
        public string list_exp_date { get; set; }
        public string list_price { get; set; }
        public string list_start_date { get; set; }
        public string make { get; set; }
        public string model { get; set; }
        public string open_bid { get; set; }
        public string state { get; set; }
        public string status { get; set; }
        public string title { get; set; }
        public string updated_at { get; set; }
        public string year { get; set; }
    }

    public class listing_images
    {
        public string created_at { get; set; }
        public string id { get; set; }
        public string listing_id { get; set; }
        public string listing_image_content_type { get; set; }
        public string listing_image_file_name { get; set; }
        public int listing_image_file_size { get; set; }
        public string listing_image_updated_at { get; set; }
        public string updated_at { get; set; }

    }

}

コードは

 listing bid1 =
              new listing { category = "electronics", city = "BBSR" };

     listing_images bid2 =
             new listing_images {  created_at = "Instrumentation",  id = "10" };

     List<listing> obid1 = new List<listing>() { bid1};
     List<listing_images> obid2 = new List<listing_images>() { bid2 };

        //DataContractJsonSerializer serializer =  null;

     string sJSON = JsonConvert.SerializeObject(obid1);
     string sJSONw = JsonConvert.SerializeObject(obid2);
4

3 に答える 3

2

DataContractJsonSerializer クラスは非常に便利です。

System.Runtime.Serialization.dll および System.Servicemodel.Web.dll への参照をプロジェクトに追加します。

using System.Runtime.Serialization.Json;

...
...
...

    MemoryStream stream = new MemoryStream();
    DataContractJsonSerializer sr = new DataContractJsonSerializer(obj.GetType());

    sr.WriteObject(stream, obj);
    stream.Position = 0;

    StreamReader reader = new StreamReader(stream);
    string jsonResult = reader.ReadToEnd();

もちろん、適切な例外処理を行います。

于 2012-04-04T12:47:15.827 に答える
1

ここに画像の説明を入力私はJSON.net @ http://json.codeplex.com/を使用することに投票します.Microsoftによって採用されており、DataContractJsonSerializerよりも効率的です.

ここでの使用例: http://james.newtonking.com/projects/json/help/

シリアライザーのパフォーマンス

またはJavascriptシリアライザーを使用しない場合

 protected static string JsonSerialize(Object obj)
 {
        var serializer = new System.Web.Script.Serialization.JavaScriptSerializer { MaxJsonLength = int.MaxValue };
        var json = serializer.Serialize(obj);
        return json;
  }
于 2012-04-04T12:54:13.283 に答える
0

これをコメントとして残しておきますが、かなり面倒でした:

System.Web.MVC への参照をプロジェクトに追加し、次のような JSON を作成します。

var jsonOutput = Json(obid1);

これは、AJAX 呼び出し用に MVC コントローラーで JSON を生成する方法です。ただし、Windows モバイル アプリで試したことはありません。

ちょっとした考え。

于 2012-04-04T12:03:27.330 に答える