I'm trying to create a way to convert an entire json file into my custom datatype which is called a product
{
"unique_image_url_prefixes":[
],
"products_and_categories":{
"Tops/Sweaters":[
{
"name":"Knit Stripe S/S Raglan Top",
"id":302418,
"image_url":"//d17ol771963kd3.cloudfront.net/129807/ca/_9UoFPZi8Zs.jpg",
"image_url_hi":"//d17ol771963kd3.cloudfront.net/129807/rc/_9UoFPZi8Zs.jpg",
"price":9900,
"sale_price":0,
"new_item":true,
"position":10,
"category_name":"Tops/Sweaters",
"price_euro":11600,
"sale_price_euro":0
}
],
"Shirts":[
{
"name":"Supreme®/Comme des Garçons SHIRT® Eyes Rayon Shirt",
"id":302426,
"image_url":"//d17ol771963kd3.cloudfront.net/132067/ca/9O934PRlIcw.jpg",
"image_url_hi":"//d17ol771963kd3.cloudfront.net/132067/rc/9O934PRlIcw.jpg",
"price":25800,
"sale_price":0,
"new_item":true,
"position":3,
"category_name":"Shirts",
"price_euro":29800,
"sale_price_euro":0
}
]
}
}
This only shows a few parts of the file the entire source is here, PasteBin
So what I'm trying to do is I convert each part of this (It's a product/item)
{
"name":"Knit Stripe S/S Raglan Top",
"id":302418,
"image_url":"//d17ol771963kd3.cloudfront.net/129807/ca/_9UoFPZi8Zs.jpg",
"image_url_hi":"//d17ol771963kd3.cloudfront.net/129807/rc/_9UoFPZi8Zs.jpg",
"price":9900,
"sale_price":0,
"new_item":true,
"position":10,
"category_name":"Tops/Sweaters",
"price_euro":11600,
"sale_price_euro":0
}
To my custom datatype called Product
public Product(string id_p, string name_p, string image_url_p, string category_name_p)
{
id = id_p;
name = name_p;
image_url = image_url_p;
category_name = category_name_p;
}
Then after that I'm trying to compare each of these values to a value assigned by the user of my program
List<Product> products = JsonConvert.DeserializeObject<List<Product>>(JsonString);
if (products == null) return null;
foreach (var product in products)
{
// ownerform.Log(product.name, 0);
if (product.category_name == T.item.Category && product.name.ToLower().Contains(T.item.Keyword.ToLower()))
{
ownerform.Log(string.Format("Resolved Keyword! Item : {0}", product.name), T.TID);
return new GetUrlResponse(true, product);
}
}
What I'm doing right now does not work and I really can't find what does.
A good way to use JSON in C# is with JSON.NET
you can find API Documentation from JSON.NET - Official site helps you work with it.
this is an example of converting an entire JSON file into your custom datatype... for example: user this is just an easy example for you to understand better the solution.
{ "user" : { "name" : "asdf", "teamname" : "b", "email" : "c" } }
so the c# would looks like:
public class User
{
public User(string json)
{
JObject jObject = JObject.Parse(json);
JToken jUser = jObject["user"];
name = (string) jUser["name"];
teamname = (string) jUser["teamname"];
email = (string) jUser["email"];
}
public string name { get; set; }
public string teamname { get; set; }
public string email { get; set; }
}
// Use
private void Run()
{
string json = @"{""user"":{""name"":""asdf"",""teamname"":""b"",""email"":""c""}";
User user = new User(json);
Console.WriteLine("Name : " + user.name);
Console.WriteLine("Teamname : " + user.teamname);
Console.WriteLine("Email : " + user.email);
}
The exception you got indicates that there's something wrong with your deserialization.
I can't tell what you actually did, but if you tried to deserialize the whole json object (ie, the whole file), you should understand that it doesn't contain a list of Products, but a more complex types' hierarchy. Thus, this line fails:
List<Product> products = JsonConvert.DeserializeObject<List<Product>>(JsonString);
I'll list two options to get things to work for you, choose whatever you feel more comfortable with.
Note that I'll use a more concise json object. In addition, I've added another object to each of the arrays, to make things more realistic (as in your file)):
{
"unique_image_url_prefixes": [],
"products_and_categories": {
"Tops": [
{
"name": "Top1",
"id": "1"
},
{
"name": "Top2",
"id": "2"
}
],
"Shirts": [
{
"name": "Shirt1",
"id": "3"
},
{
"name": "Shirt2",
"id": "4"
}
]
}
}
Option #1
Build The Class Hierarchy
Go to the post @CodingYoshi linked to, and create the class hierarchy with Visual Studio. You'll get something very similar the following hierarchy, though I'll make it more concise:
public class Rootobject
{
public object[] unique_image_url_prefixes { get; set; }
public Products_And_Categories products_and_categories { get; set; }
}
public class Products_And_Categories
{
public TopsSweaters[] TopsSweaters { get; set; }
public Shirts[] Shirts { get; set; }
}
public class TopsSweaters
{
public string name { get; set; }
public int id { get; set; }
}
public class Shirts
{
public string name { get; set; }
public int id { get; set; }
}
Deserialization
This is how you deserialize your string:
string JsonString = @"{""unique_image_url_prefixes"":[],""products_and_categories"":{""TopsSweaters"":[{""name"":""Top1"",""id"":""1""},{""name"":""Top2"",""id"":""2""}],""Shirts"":[{""name"":""Shirt1"",""id"":""3""},{""name"":""Shirt2"",""id"":""4""}]}}";
Rootobject container = JsonConvert.DeserializeObject<Rootobject>(JsonString);
You can then iterate over each array ( TopsSweaters
, Shirts
) within container.products_and_categories
.
Option #2
If you'd like to use some other class hierarchy (eg, your Product
class), you can do the following:
Build The Class Hierarchy
public class Product
{
[JsonProperty("name")]
public string name { get; set; }
[JsonProperty("id")]
public int id { get; set; }
}
// Choose either one of the following (they function the same):
public class ProductsContainer1
{
[JsonProperty("TopsSweaters")]
public List<Product> ProductsList1 { get; set;}
[JsonProperty("Shirts")]
public List<Product> ProductsList2 { get; set;}
}
public class ProductsContainer2
{
public List<Product> TopsSweaters { get; set; }
public List<Product> Shirts { get; set; }
}
// Choose either one of the following (they function the same):
public class Things1
{
[JsonProperty("unique_image_url_prefixes")]
public object[] Prefixes { get; set;}
[JsonProperty("products_and_categories")]
public ProductsContainer1 Products { get; set;}
}
public class Things2
{
public object[] unique_image_url_prefixes { get; set;}
public ProductsContainer2 products_and_categories { get; set;}
}
Deserialization
Deserialize as follows:
string JsonString = @"{""unique_image_url_prefixes"":[],""products_and_categories"":{""TopsSweaters"":[{""name"":""Top1"",""id"":""1""},{""name"":""Top2"",""id"":""2""}],""Shirts"":[{""name"":""Shirt1"",""id"":""3""},{""name"":""Shirt2"",""id"":""4""}]}}";
// Use either one of the following:
Things1 container1 = JsonConvert.DeserializeObject<Things1>(JsonString);
Things2 container2 = JsonConvert.DeserializeObject<Things2>(JsonString);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.