Reputation: 820
Hi I am working on web API and getting the result on StreamReader
. I want to convert these StreamReader
to JSON
I am using the Following code :
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());
}
And the actual Model schema is something like that
{
"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": ""
}
]
}
And model is something like this
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)
}
But i have no idea how do I create the Objects to get the JSON
result.I am trying with the Myobject
class but getting the error:
Type 'System.String' is not supported for deserialization of an array.
Please help how do i get the JSON
result.Thanks in advance.
Upvotes: 0
Views: 201
Reputation: 10824
Yo can use JSON.NET for parsing the json result:
var data = JsonConvert.DeserializeObject<Rootobject>(data);
With this approach you don't need to fully deserialize the JSON object
Updated:
var objText = reader.ReadToEnd();
var data = JsonConvert.DeserializeObject<Rootobject>(objText);
PS: I used Paste JSON as Classes in Visual Studio for generating the models, Or you can use json2csharp for creating the classes.
Upvotes: 2
Reputation: 1449
Using json2csharp
you can create model class from json text.I copied your json text and what i got as output is below class.
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; }
}
Use Json.Net
and you can deserialize json text into an object using following code
var objText = reader.ReadToEnd();
var myojb = JsonConvert.DeserializeObject<RootObject>(objText);
For more information check Serializing and Deserializing JSON with Json.NET
Hope it helps.
Upvotes: 0