字符串转Json对象

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;

namespace JsonTest
{
class Program
{
static void Main(string[] args)
{
string json = GetJsonString();
Console.WriteLine(json);

Console.ReadKey();
}

private static string GetJsonString()
{
List<Product> products = new List<Product>()
{
new Product(){Name="苹果",Price=5.5},
new Product(){Name="橘子",Price=2.5},
new Product(){Name="干柿子",Price=16.00}
};

ProductList productlist = new ProductList();
productlist.GetProducts = products;
return (new JavaScriptSerializer().Serialize(productlist));
}
}

public class Product
{
public string Name { get; set; }

public double Price { get; set; }

}

public class ProductList
{
public List<Product> GetProducts { get; set; }
}

}

上一篇:移动开发之【微信小程序】的原理与权限问题以及相关的简易教程


下一篇:PTA題目的處理(一)