解析反序列化json返回数据

_VINCE_ 2014-03-03 10:54:37

如图,返回json数据,如何解析出来呢?
{\"retCode\":0,\"infoFeeRates\":[{\"feePrefix\":\"\",\"areaCode\":\"09\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"08\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"07\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"06\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"05\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"04\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"03\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"02\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"01\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"9\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"8\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"7\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"6\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"5\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"4\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"3\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"2\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"1\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6}]}\r\n



有什么通用的方法么??
...全文
288 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
_VINCE_ 2014-03-03
  • 打赏
  • 举报
回复
引用 9 楼 yyl8781697 的回复:
这样的数据你直接字符串拼接就好了呀 为何要用JsonData来生成
想做成一个比较通用的。不过还是谢谢你啊。
zj25810 2014-03-03
  • 打赏
  • 举报
回复
var json = new JavaScriptSerializer(); object obj = json.DeserializeObject(yxq);
bdmh 2014-03-03
  • 打赏
  • 举报
回复
下载Newtonsoft.Json.dll
yyl8781697 2014-03-03
  • 打赏
  • 举报
回复
这样的数据你直接字符串拼接就好了呀 为何要用JsonData来生成
_VINCE_ 2014-03-03
  • 打赏
  • 举报
回复
引用 7 楼 yyl8781697 的回复:

using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
//LitJson下载地址:http://dl.oschina.net/soft/litjson
using LitJson;

namespace ConsoleApplicationTest
{
    /// <summary>
    /// json序列化的实体类
    /// </summary>
    class Info
    {
        public string feePrefix { get; set; }

        public string areaCode { get; set; }

        public int type { get; set; }

        public string areaName { get; set; }

        public List<string> infoFeeRateSections { get; set; }

        public double fee { get; set; }

        public int period { get; set; }

        public int lockType { get; set; }

        public double ivrFee { get; set; }

        public int ivrPeriod { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            string jsonStr = "{\"retCode\":0,\"infoFeeRates\":[{\"feePrefix\":\"\",\"areaCode\":\"09\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[\"sect1\",\"sect2\"],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"08\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"07\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"06\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"05\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"04\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"03\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"02\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"01\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"9\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"8\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"7\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"6\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"5\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"4\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"3\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"2\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"1\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6}]}";

            JsonData jsonData = JsonMapper.ToObject(jsonStr);

            if ("0".Equals(Convert.ToString(jsonData["retCode"])))//根据你的情况判断
            {
                //Console.WriteLine(jsonData["infoFeeRates"][0]["areaCode"]);//其实在这里可以直接取数据了  现在取得是第一个的areaCode

                List<Info> list = JsonMapper.ToObject<List<Info>>(jsonData["infoFeeRates"].ToJson());//在这里把之前反序列出来的数组再tostrng掉,然后又序列化成List   两次反序列化之后效率肯定没有上面的直接取数组的值来的高
            }

            Console.ReadKey();
        }

        
    }
}
自己看吧
谢谢。会了。再请教下。如果我想组成json数据发送 想 string postData = "{\"e164s\":[\"888888\"]}"; 这样的格式 我看了下。 JsonWriter writer = new JsonWriter(); writer.WriteArrayStart(); writer.Write("888888"); writer.WriteArrayEnd(); 这样可以变成{["888888"]} 但是我怎么把e164s和他组合一起呢? JsonData jd2 = new JsonData(); jd2["e164s"] = "888888"; string json1 = jd2.ToJson(); 这样写就变成了"{\"e164s\":\"888888\"}" 但是我想要的结果是{\"e164s\":[\"888888\"]}
yyl8781697 2014-03-03
  • 打赏
  • 举报
回复



using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
//LitJson下载地址:http://dl.oschina.net/soft/litjson
using LitJson;

namespace ConsoleApplicationTest
{
/// <summary>
/// json序列化的实体类
/// </summary>
class Info
{
public string feePrefix { get; set; }

public string areaCode { get; set; }

public int type { get; set; }

public string areaName { get; set; }

public List<string> infoFeeRateSections { get; set; }

public double fee { get; set; }

public int period { get; set; }

public int lockType { get; set; }

public double ivrFee { get; set; }

public int ivrPeriod { get; set; }
}

class Program
{
static void Main(string[] args)
{
string jsonStr = "{\"retCode\":0,\"infoFeeRates\":[{\"feePrefix\":\"\",\"areaCode\":\"09\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[\"sect1\",\"sect2\"],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"08\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"07\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"06\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"05\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"04\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"03\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"02\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"01\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"9\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"8\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"7\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"6\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"5\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"4\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"3\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"2\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6},{\"feePrefix\":\"\",\"areaCode\":\"1\",\"type\":4,\"areaName\":\"\",\"infoFeeRateSections\":[],\"fee\":0.0030,\"period\":6,\"lockType\":0,\"ivrFee\":0.0030,\"ivrPeriod\":6}]}";

JsonData jsonData = JsonMapper.ToObject(jsonStr);

if ("0".Equals(Convert.ToString(jsonData["retCode"])))//根据你的情况判断
{
//Console.WriteLine(jsonData["infoFeeRates"][0]["areaCode"]);//其实在这里可以直接取数据了 现在取得是第一个的areaCode

List<Info> list = JsonMapper.ToObject<List<Info>>(jsonData["infoFeeRates"].ToJson());//在这里把之前反序列出来的数组再tostrng掉,然后又序列化成List 两次反序列化之后效率肯定没有上面的直接取数组的值来的高
}

Console.ReadKey();
}


}
}


自己看吧
_VINCE_ 2014-03-03
  • 打赏
  • 举报
回复
引用 5 楼 yyl8781697 的回复:
[]代表数组,可以正常序列化的 在litjson中由他自带的数据类型JsonData,他可以装载你全部的数据(里面包含int,string,array,dictory等), 当然你在反序列化的时候可以自己直接指定类(使用泛型)
能写个数组序列化的简单的例子么。一直搞不定。反序列化到是网上有例子。
yyl8781697 2014-03-03
  • 打赏
  • 举报
回复
[]代表数组,可以正常序列化的 在litjson中由他自带的数据类型JsonData,他可以装载你全部的数据(里面包含int,string,array,dictory等), 当然你在反序列化的时候可以自己直接指定类(使用泛型)
_VINCE_ 2014-03-03
  • 打赏
  • 举报
回复
引用 3 楼 yyl8781697 的回复:
使用LitJson也不错,看下这里有相关文章
看了下。他序列化的例子。都是{"book":{"title":"android game!","author":"pei","bookdetail":{"pages":429,"about":null}}} 但是我想里面的{}变成[ ] 应该怎么做?
yyl8781697 2014-03-03
  • 打赏
  • 举报
回复
使用LitJson也不错,看下这里有相关文章

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧