C# 如何在c中创建JSON文本的对象#
您好,我正在开发web API,并在C# 如何在c中创建JSON文本的对象#,c#,json,C#,Json,您好,我正在开发web API,并在StreamReader上获得结果。我想将这些StreamReader转换为JSON我正在使用以下代码: var response = (HttpWebResponse)request.GetResponse(); using (var stream = response.GetResponseStream()) using (var reader = new StreamReader(stream))
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());
}
实际的模型模式是这样的
{
"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
结果。提前感谢。使用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; }
}
有关更多信息,请查看
希望有帮助。Yo可以用来解析json结果:
var data = JsonConvert.DeserializeObject<Rootobject>(data);
var data=JsonConvert.DeserializeObject(数据);
使用这种方法,您不需要完全反序列化JSON对象
已更新:
var objText = reader.ReadToEnd();
var data = JsonConvert.DeserializeObject<Rootobject>(objText);
var objText=reader.ReadToEnd();
var data=JsonConvert.DeserializeObject(objText);
PS:我在Visual Studio中用于生成模型,也可以用于创建类。以及如何创建对象属性?尝试上述操作,模型类的所有属性值将存储在
MyObject
中,并且在设置断点时,您将能够看到所有的值。但是当它到达这一行时..我直接得到了一个错误,即数组的反序列化不支持“Type”System.String“你能分享一下objText
的价值吗?和我在使用JSON
view@Gitz这是一个字符串形式的JSON数据。我把这行放在哪里??您能解释一下吗?我想您应该添加“objText”作为data.var data=JsonConvert.DeserializeObject(objText);我使用的是和Newtonsoft.Json
但是在@Sirwan affic中找不到Rootobject
,您可以从读卡器流发布转储吗?也许这是不正确的?
var objText = reader.ReadToEnd();
var data = JsonConvert.DeserializeObject<Rootobject>(objText);