当前位置:编程学习 > C#/ASP.NET >>

字符串数组转换

字符串:"[[1,false],[2,false],[3,false]]"
想把他转换成list之类的.. --------------------编程问答-------------------- 这是json格式,你用json类解析,如果是4.5以下的.net,可以到网上找一个类,比如Newtonsoft.Json.dll --------------------编程问答-------------------- 楼上的方法不错,那个dll支持list转json,json转list等方法,非常方便 --------------------编程问答-------------------- 我用了JsonConvert.DeserializeObject 但提示错误。。
Cannot deserialize the current JSON array。。。 --------------------编程问答-------------------- To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array.  --------------------编程问答-------------------- [{1,false},{2,false},{3,false}]
那个不像json --------------------编程问答-------------------- 我也觉得不像,,json key value 模式。。这个格式更简单吧,不知道怎么转换 --------------------编程问答-------------------- 可以把那个替换成json数组再做啊,先去掉头尾的[]  然后替换[]为{},再加上[],最后再做转换 --------------------编程问答--------------------  - -  。   
跟JSON十分类似了。  不过JSON需要用{}来包裹里面的内容 。  而且要求是key:value对应。 
LZ的这个需要改动比较多的格式才能用JSON来转化。 

{1:false,2:false,3:false}

这样子 JSON就能识别了。   1 2 3也会排列好- -  --------------------编程问答-------------------- 我也来学习学习 --------------------编程问答--------------------
Split + Replace 可以做吗 --------------------编程问答-------------------- 不是标准的json。直接splid分割不行? --------------------编程问答-------------------- 做成JSON的格式例如 {name:"ka",age:"123",address:"CSDN"}
这样的
给你个代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text;
/// <summary>
///JSON 的摘要说明
/// </summary>
public class JSONHelper
{
    /// <summary>
    /// 序例化JSON数据
    /// </summary>
    public JSONHelper()
{

}
    /// <summary>
    /// 序例化对象转换成JSON
    /// </summary>
    /// <typeparam name="T">序例化的类型</typeparam>
    /// <param name="t">需要序例化的对象</param>
    /// <returns>JSON数据格式</returns>
    public static string JsonSerializtion<T>(T t)
    {
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
        using (MemoryStream ms = new MemoryStream())
        {
            serializer.WriteObject(ms, t);
            string json = Encoding.UTF8.GetString(ms.ToArray());
            return json;
        }
    }
    /// <summary>
    /// 反序例化转换成对象
    /// </summary>
    /// <typeparam name="T">转换的类型</typeparam>
    /// <param name="jsonString">需要转化的JSON的数据</param>
    /// <returns>数据对象</returns>
    public static T JsonDeserialization<T>(string jsonString)
    {
       
        using (MemoryStream jsonStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
        {

            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));

                T obj = (T)serializer.ReadObject(jsonStream);
                int i = serializer.MaxItemsInObjectGraph;
               
            return obj;
        }
    }
}
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,