簡體   English   中英

如何在C#中創建JSON文本對象

[英]How to create objects of JSON text in c#

嗨,我正在使用Web API,並在StreamReader上獲取結果。 我想將這些StreamReader轉換為JSON我正在使用以下代碼:

 var response = (HttpWebResponse)request.GetResponse();
            using (var stream = response.GetResponseStream())
            using (var reader = new StreamReader(stream))
            {
                JavaScriptSerializer js = new JavaScriptSerializer();
                var objText = reader.ReadToEnd();
                MyObject myojb = (MyObject)js.Deserialize(objText, typeof(MyObject));

                // Response.Write(reader.ReadToEnd());
            } 

實際的Model模式就是這樣

{
  "id": "",
  "description": "",
  "added_date": "",
  "media_type": "",
  "contributor": {
    "id": ""
  },
  "aspect": 0,
  "image_type": "",
  "is_editorial": false,
  "is_adult": false,
  "is_illustration": false,
  "has_model_release": false,
  "has_property_release": false,
  "releases": [
    ""
  ],
  "categories": [
    {
      "id": "",
      "name": ""
    }
  ],
  "keywords": [
    ""
  ],
  "assets": {
    "small_jpg": {
      "height": 0,
      "width": 0,
      "file_size": 0,
      "display_name": "",
      "dpi": 0,
      "format": "",
      "is_licensable": false
    },
    "medium_jpg": {
      "height": 0,
      "width": 0,
      "file_size": 0,
      "display_name": "",
      "dpi": 0,
      "format": "",
      "is_licensable": false
    },
    "huge_jpg": {
      "height": 0,
      "width": 0,
      "file_size": 0,
      "display_name": "",
      "dpi": 0,
      "format": "",
      "is_licensable": false
    },
    "supersize_jpg": {
      "height": 0,
      "width": 0,
      "file_size": 0,
      "display_name": "",
      "dpi": 0,
      "format": "",
      "is_licensable": false
    },
    "huge_tiff": {
      "height": 0,
      "width": 0,
      "file_size": 0,
      "display_name": "",
      "dpi": 0,
      "format": "",
      "is_licensable": false
    },
    "supersize_tiff": {
      "height": 0,
      "width": 0,
      "file_size": 0,
      "display_name": "",
      "dpi": 0,
      "format": "",
      "is_licensable": false
    },
    "vector_eps": {
      "height": 0,
      "width": 0,
      "file_size": 0,
      "display_name": "",
      "dpi": 0,
      "format": "",
      "is_licensable": false
    },
    "small_thumb": {
      "url": "",
      "height": 0,
      "width": 0
    },
    "large_thumb": {
      "url": "",
      "height": 0,
      "width": 0
    },
    "preview": {
      "url": "",
      "height": 0,
      "width": 0
    },
    "preview_1000": {
      "url": "",
      "height": 0,
      "width": 0
    }
  },
  "models": [
    {
      "id": ""
    }
  ]
}

模型就是這樣

Image {
id (string),
description (string, optional),
added_date (string, optional),
media_type (string),
contributor (Contributor),
aspect (number, optional),
image_type (string, optional),
is_editorial (boolean, optional),
is_adult (boolean, optional),
is_illustration (boolean, optional),
has_model_release (boolean, optional),
has_property_release (boolean, optional),
releases (array[string], optional),
categories (array[Category], optional),
keywords (array[string], optional),
assets (ImageAssets, optional),
models (array[Model], optional)
}
Contributor {
id (string)
}
Category {
id (string, optional),
name (string, optional)
}
ImageAssets {
small_jpg (ImageSizeDetails, optional),
medium_jpg (ImageSizeDetails, optional),
huge_jpg (ImageSizeDetails, optional),
supersize_jpg (ImageSizeDetails, optional),
huge_tiff (ImageSizeDetails, optional),
supersize_tiff (ImageSizeDetails, optional),
vector_eps (ImageSizeDetails, optional),
small_thumb (Thumbnail, optional),
large_thumb (Thumbnail, optional),
preview (Thumbnail, optional),
preview_1000 (Thumbnail, optional)
}
ImageSizeDetails {
height (integer, optional),
width (integer, optional),
file_size (integer, optional),
display_name (string, optional),
dpi (integer, optional),
format (string, optional),
is_licensable (boolean, optional)
}
Thumbnail {
url (string),
height (integer),
width (integer)
}
Model {
id (string)
}

但我不知道如何創建對象以獲取JSON結果。我正在嘗試Myobject類,但收到錯誤:

數組反序列化不支持類型“ System.String”。

請幫助我如何獲取JSON結果。謝謝。

可以使用JSON.NET解析json結果:

var data = JsonConvert.DeserializeObject<Rootobject>(data);

使用這種方法,您不需要完全反序列化JSON對象

更新時間

var objText = reader.ReadToEnd();
var data = JsonConvert.DeserializeObject<Rootobject>(objText);

PS :我在Visual Studio中使用粘貼JSON作為類來生成模型,或者可以使用json2csharp創建類。

使用json2csharp您可以從json文本創建模型類。我復制了您的json文本,我得到的輸出是在類下面。

public class Contributor
{
   public string id { get; set; }
}

public class Category
{
public string id { get; set; }
public string name { get; set; }
}

public class SmallJpg
{
public int height { get; set; }
public int width { get; set; }
public int file_size { get; set; }
public string display_name { get; set; }
public int dpi { get; set; }
public string format { get; set; }
public bool is_licensable { get; set; }
}

public class MediumJpg
{
public int height { get; set; }
public int width { get; set; }
public int file_size { get; set; }
public string display_name { get; set; }
public int dpi { get; set; }
public string format { get; set; }
public bool is_licensable { get; set; }
}

public class HugeJpg
{
public int height { get; set; }
public int width { get; set; }
public int file_size { get; set; }
public string display_name { get; set; }
public int dpi { get; set; }
public string format { get; set; }
public bool is_licensable { get; set; }
}

public class SupersizeJpg
{
public int height { get; set; }
public int width { get; set; }
public int file_size { get; set; }
public string display_name { get; set; }
public int dpi { get; set; }
public string format { get; set; }
public bool is_licensable { get; set; }
}

public class HugeTiff
{
public int height { get; set; }
public int width { get; set; }
public int file_size { get; set; }
public string display_name { get; set; }
public int dpi { get; set; }
public string format { get; set; }
public bool is_licensable { get; set; }
}

 public class SupersizeTiff
{
public int height { get; set; }
public int width { get; set; }
public int file_size { get; set; }
public string display_name { get; set; }
public int dpi { get; set; }
public string format { get; set; }
public bool is_licensable { get; set; }
}

 public class VectorEps
 {
public int height { get; set; }
public int width { get; set; }
public int file_size { get; set; }
public string display_name { get; set; }
public int dpi { get; set; }
public string format { get; set; }
public bool is_licensable { get; set; }
}

public class SmallThumb
{
public string url { get; set; }
public int height { get; set; }
public int width { get; set; }
}

public class LargeThumb
{
public string url { get; set; }
public int height { get; set; }
public int width { get; set; }
}

public class Preview
{
public string url { get; set; }
public int height { get; set; }
public int width { get; set; }
}

public class Preview1000
{
public string url { get; set; }
public int height { get; set; }
public int width { get; set; }
}

public class Assets
{
public SmallJpg small_jpg { get; set; }
public MediumJpg medium_jpg { get; set; }
public HugeJpg huge_jpg { get; set; }
public SupersizeJpg supersize_jpg { get; set; }
public HugeTiff huge_tiff { get; set; }
public SupersizeTiff supersize_tiff { get; set; }
public VectorEps vector_eps { get; set; }
public SmallThumb small_thumb { get; set; }
public LargeThumb large_thumb { get; set; }
public Preview preview { get; set; }
public Preview1000 preview_1000 { get; set; }
}

public class Model
{
public string id { get; set; }
}

public class RootObject
{
public string id { get; set; }
public string description { get; set; }
public string added_date { get; set; }
public string media_type { get; set; }
public Contributor contributor { get; set; }
public int aspect { get; set; }
public string image_type { get; set; }
public bool is_editorial { get; set; }
public bool is_adult { get; set; }
public bool is_illustration { get; set; }
public bool has_model_release { get; set; }
public bool has_property_release { get; set; }
public List<string> releases { get; set; }
public List<Category> categories { get; set; }
public List<string> keywords { get; set; }
public Assets assets { get; set; }
public List<Model> models { get; set; }
}

使用Json.Net

您可以使用以下代碼將json文本反序列化為對象

var objText = reader.ReadToEnd();
var myojb = JsonConvert.DeserializeObject<RootObject>(objText);

有關更多信息,請檢查使用Json.NET序列化和反序列化JSON。

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM