0

以下のコードはwebRequestからの応答を取得しています。

HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;

using (var twitpicResponse = (HttpWebResponse)request.GetResponse())
{
    using (var reader = new StreamReader(twitpicResponse.GetResponseStream()))
    {
        JavaScriptSerializer js = new JavaScriptSerializer();

        var objText = reader.ReadToEnd();

        while (reader.EndOfStream)
        {
            objText = reader.ReadLine();

            MyObject myobj= (MyObject)js.Deserialize<MyObject>(objText);
        }
    }
}

以下の形式で JSON 応答を取得しています。この JSON 応答を myobj にバインドしたいと考えています。

{"Results":[{"Id":"571611","ModerationStatus":"APPROVED","LastModificationTime":
        "2011-10-31T05:36:33-05:00"
        ,"IsRatingsOnly":true,"TotalCommentCount":0,"Rating":5,"RatingRange":5,"IsRecommended":true,"TotalFeedbackCount":0,"TotalPositiveFeedbackCount":0,"TotalNegativeFeedbackCount":0,"DisplayLocale":"en_US","SubmissionTime":
        "2011-05-25T00:00:00-05:00"
        ,"IsFeatured":false,"LastModeratedTime":
        "2011-10-31T05:36:33-05:00"
        ,"ProductId":"3240234","AuthorId":"z7knrbjunvi022pe4swqp18fra"},{"Id":"571612","ModerationStatus":"APPROVED","LastModificationTime":
        "2011-10-31T05:36:33-05:00"
        ,"IsRatingsOnly":true,"TotalCommentCount":0,"Rating":5,"RatingRange":5,"IsRecommended":true,"TotalFeedbackCount":0,"TotalPositiveFeedbackCount":0,"TotalNegativeFeedbackCount":0,"DisplayLocale":"en_US","SubmissionTime":
        "2011-05-25T00:00:00-05:00"
        ,"IsFeatured":false,"LastModeratedTime":
        "2011-10-31T05:36:33-05:00"
        ,"ProductId":"3240234","AuthorId":"zsknvekklaxl56nzwliultz5mp"}],"Includes":{"Products":{"3240234":{"Id":"3240234","Name":"Rosner Chevrolet","Description":"Brevard County","BrandExternalId":"01","Brand":{"Id":"01","Name":"Personal Accounts"},"CategoryId":"324","ProductPageUrl":"https://www.sccu.com/personal/vehicle-loans/dealer-review.asp?did=234&dealerName=Rosner+Chevrolet"}},"Authors":{"z7knrbjunvi022pe4swqp18fra":{"Id":"z7knrbjunvi022pe4swqp18fra","ModerationStatus":"NONE"},"zsknvekklaxl56nzwliultz5mp":{"Id":"zsknvekklaxl56nzwliultz5mp","ModerationStatus":"NONE"}}},"HasErrors":false,"Offset":0,"Limit":2,"TotalResults":25}

この JSON ブロック (複数の値を持つこの JSON ブロック) を 1 つずつ読み取り、objectlist に格納します。

4

3 に答える 3

2

このようにして、最初にJson文字列をここで解析してみてください\

http://json.parser.online.fr/

そこに合格した場合は、 http://json2csharp.com/に移動し ます...文字列を貼り付けます...属性を持つクラスを生成します

それから

クラスをコード内に貼り付けます。クラスの新しいオブジェクトを作成します

Deserialize を呼び出す

   JavaScriptSerializer js = new JavaScriptSerializer();
   var Data = DeserializeFromJson<RootObject>("Json String");
 public T DeserializeFromJson<T>(string json)
    {
        System.Web.Script.Serialization.JavaScriptSerializer ObjJSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        T deserializedProduct = ObjJSerializer.Deserialize<T>(json);
        return deserializedProduct;
    }


public class Result
{
public string Id { get; set; }
public string ModerationStatus { get; set; }
public string LastModificationTime { get; set; }
public bool IsRatingsOnly { get; set; }
public int TotalCommentCount { get; set; }
public int Rating { get; set; }
public int RatingRange { get; set; }
public bool IsRecommended { get; set; }
public int TotalFeedbackCount { get; set; }
public int TotalPositiveFeedbackCount { get; set; }
public int TotalNegativeFeedbackCount { get; set; }
public string DisplayLocale { get; set; }
public string SubmissionTime { get; set; }
public bool IsFeatured { get; set; }
public string LastModeratedTime { get; set; }
public string ProductId { get; set; }
public string AuthorId { get; set; }
 }

  public class Brand
  {
public string Id { get; set; }
public string Name { get; set; }
  }

  public class __invalid_type__3240234
 {
public string Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string BrandExternalId { get; set; }
public Brand Brand { get; set; }
public string CategoryId { get; set; }
public string ProductPageUrl { get; set; }
   }

public class Products
{
public __invalid_type__3240234 __invalid_name__3240234 { get; set; }
 }

 public class Z7knrbjunvi022pe4swqp18fra
{
public string Id { get; set; }
public string ModerationStatus { get; set; }
}

 public class Zsknvekklaxl56nzwliultz5mp
{ 
public string Id { get; set; }
public string ModerationStatus { get; set; }
}

public class Authors
{
public Z7knrbjunvi022pe4swqp18fra z7knrbjunvi022pe4swqp18fra { get; set; }
public Zsknvekklaxl56nzwliultz5mp zsknvekklaxl56nzwliultz5mp { get; set; }
 }

 public class Includes
{
public Products Products { get; set; }
public Authors Authors { get; set; }
}

public class RootObject
{
public List<Result> Results { get; set; }
public Includes Includes { get; set; }
public bool HasErrors { get; set; }
public int Offset { get; set; }
public int Limit { get; set; }
public int TotalResults { get; set; }
}
于 2013-10-30T10:56:57.130 に答える
0

JSON.Netを使用して、1 行のコードでこれを C# オブジェクトに読み込むことができます。

static void Main(string[] args)
{
    var x = JsonConvert.DeserializeObject(JsonString);
}

private static string JsonString
{
    get
    {
        return @"{""Results"":[{""Id"":""571611"",""ModerationStatus"":""APPROVED"",""LastModificationTime"":
                ""2011-10-31T05:36:33-05:00""
                ,""IsRatingsOnly"":true,""TotalCommentCount"":0,""Rating"":5,""RatingRange"":5,""IsRecommended"":true,""TotalFeedbackCount"":0,""TotalPositiveFeedbackCount"":0,""TotalNegativeFeedbackCount"":0,""DisplayLocale"":""en_US"",""SubmissionTime"":
                ""2011-05-25T00:00:00-05:00""
                ,""IsFeatured"":false,""LastModeratedTime"":
                ""2011-10-31T05:36:33-05:00""
                ,""ProductId"":""3240234"",""AuthorId"":""z7knrbjunvi022pe4swqp18fra""},{""Id"":""571612"",""ModerationStatus"":""APPROVED"",""LastModificationTime"":
                ""2011-10-31T05:36:33-05:00""
                ,""IsRatingsOnly"":true,""TotalCommentCount"":0,""Rating"":5,""RatingRange"":5,""IsRecommended"":true,""TotalFeedbackCount"":0,""TotalPositiveFeedbackCount"":0,""TotalNegativeFeedbackCount"":0,""DisplayLocale"":""en_US"",""SubmissionTime"":
                ""2011-05-25T00:00:00-05:00""
                ,""IsFeatured"":false,""LastModeratedTime"":
                ""2011-10-31T05:36:33-05:00""
                ,""ProductId"":""3240234"",""AuthorId"":""zsknvekklaxl56nzwliultz5mp""}],""Includes"":{""Products"":{""3240234"":{""Id"":""3240234"",""Name"":""Rosner Chevrolet"",""Description"":""Brevard County"",""BrandExternalId"":""01"",""Brand"":{""Id"":""01"",""Name"":""Personal Accounts""},""CategoryId"":""324"",""ProductPageUrl"":""https://www.sccu.com/personal/vehicle-loans/dealer-review.asp?did=234&dealerName=Rosner+Chevrolet""}},""Authors"":{""z7knrbjunvi022pe4swqp18fra"":{""Id"":""z7knrbjunvi022pe4swqp18fra"",""ModerationStatus"":""NONE""},""zsknvekklaxl56nzwliultz5mp"":{""Id"":""zsknvekklaxl56nzwliultz5mp"",""ModerationStatus"":""NONE""}}},""HasErrors"":false,""Offset"":0,""Limit"":2,""TotalResults"":25}";
    }
}

MyObjectが JSON 構造と一致する場合、オブジェクトは外側の JSON オブジェクトの配列になります。次のように動作します。

var myobj = JsonConvert.DeserializeObject<MyObject>(JsonString);
于 2013-10-30T10:23:45.747 に答える