一:在C#中使用json字符串

从这里下载:http://www.newtonsoft.com/products/json/

安装:

1.解压下载文件,得到Newtonsoft.Json.dll
2.在项目中添加引用..
序列化和反序列在.net项目中:

Product product = new Product();

product.Name = "Apple";
product.Expiry = new DateTime(, , );
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" }; string output = JavaScriptConvert.SerializeObject(product);
//{
// "Name": "Apple",
// "Expiry": new Date(1230422400000),
// "Price": 3.99,
// "Sizes": [
// "Small",
// "Medium",
// "Large"
// ]
//} Product deserializedProduct = (Product)JavaScriptConvert.DeserializeObject(output, typeof(Product));

读取JSON

string jsonText = "['JSON!',1,true,{property:'value'}]";

JsonReader reader = new JsonReader(new StringReader(jsonText));

Console.WriteLine("TokenType\t\tValueType\t\tValue");

while (reader.Read())
{
Console.WriteLine(reader.TokenType + "\t\t" + WriteValue(reader.ValueType) + "\t\t" + WriteValue(reader.Value))
}

结果显示:

TokenType ValueType Value
StartArray null null
String System.String JSON!
Integer System.Int32 1
Boolean System.Boolean True
StartObject null null
PropertyName System.String property
String System.String value
EndObject null null
EndArray null null

JSON写入

StringWriter sw = new StringWriter();
JsonWriter writer = new JsonWriter(sw); writer.WriteStartArray();
writer.WriteValue("JSON!");
writer.WriteValue();
writer.WriteValue(true);
writer.WriteStartObject();
writer.WritePropertyName("property");
writer.WriteValue("value");
writer.WriteEndObject();
writer.WriteEndArray(); writer.Flush(); string jsonText = sw.GetStringBuilder().ToString(); Console.WriteLine(jsonText);
// ['JSON!',1,true,{property:'value'}]

这里会打印出: ['JSON!',1,true,{property:'value'}].

完整例子

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; //需要引用 Newtonsoft.Json.dll
using Newtonsoft.Json; namespace JsonTest
{
class Program
{
/// <summary>
/// 人员类
/// </summary>
public class Person
{
public string name; //姓名
public int age; //年龄
public bool sex_is_male; //性别 public struct Partner //伙伴
{
public string partner_name; //伙伴姓名
public int partner_age; //伙伴年龄
public bool partner_sex_is_male; //伙伴性别
}
public Partner partner; public string[] achievement; //成就
} static void Main(string[] args)
{
//设置一个Person类
Person p = new Person();
p.name = "Tsybius";
p.age = ;
p.sex_is_male = true;
p.partner.partner_name = "Galatea";
p.partner.partner_age = ;
p.partner.partner_sex_is_male = false;
p.achievement = new string[] { "ach1", "ach2", "ach3" }; //直接输出
Console.WriteLine("Formatting.None:");
string json1 = JsonConvert.SerializeObject(p);
Console.WriteLine(json1 + "\n"); //缩进输出
Console.WriteLine("Formatting.Indented:");
string json2 = JsonConvert.SerializeObject(p, Formatting.Indented);
Console.WriteLine(json2 + "\n"); Console.ReadLine();
}
}
}

二:格式

格式:

{"tools": [
{ "name":"css format" , "site":"http://www.cnblogs.com/hongmaju/" },
{ "name":"json format" , "site":"http://www.cnblogs.com/hongmaju/" },
{ "name":"hash MD5" , "site":"http://www.cnblogs.com/hongmaju/" }
]
}

有层次的格式:

{
"": [
{
"Txt": "['收银员']",
"Names": "['bi']",
"data": [
{
"ID": "",
"Name": "收银员"
},
{
"ID": "",
"Name": "张三"
}
]
}
],
"": [
{
"Txt": "['餐台类型']",
"Names": "['ro']",
"data": [
{
"ID": "",
"Name": "A1"
},
{
"ID": "",
"Name": "包桌"
}
]
}
],
"": [
{
"Txt": "['账单类型']",
"Names": "['bi']",
"data": [
{
"ID": "G",
"Name": "挂账账单"
},
{
"ID": "gzhk",
"Name": "挂账回款"
},
{
"ID": "H",
"Name": "会员充值"
},
{
"ID": "l",
"Name": "连锁账单"
},
{
"ID": "M",
"Name": "招待账单"
},
{
"ID": "R",
"Name": "红冲账单"
},
{
"ID": "Z",
"Name": "异地卡充值"
},
{
"ID": "J",
"Name": "预订押金"
},
{
"ID": "Z",
"Name": "已结账单"
}
]
}
],
"": [
{
"Txt": "['营业站点']",
"Names": "['se']",
"data": [
{
"ID": "",
"Name": "重庆万州面馆"
}
]
}
]
}

在线验证json格式的地址:http://www.atool.org/jsonformat.php

三:常用的使用方法

对上面的json的处理:

success: function (result) {
  for (var key in result) {
    alert(key);//弹出0,1,2,3这几个数字
  alert(result[key]);//弹出0对应的json串
   alert(getValue(result[key][0].Txt));//得到0对应的json串中的Txt的值
    for (var da in result[key]) {
    alert(result[key][da].data);//得到0对应的json串中的data的json串
    }
  }
}

json串的使用的更多相关文章

  1. iOS 字典或者数组和JSON串的转换

    在和服务器交互过程中,会iOS 字典或者数组和JSON串的转换,具体互换如下: // 将字典或者数组转化为JSON串 + (NSData *)toJSONData:(id)theData { NSEr ...

  2. [原创] C# dynamic拼接Json串

    using Newtonsoft.Json; 之前拼接两个json串,是用的这样的代码 , json1.Length - ); json2 = json2.Insert(json2 - , tmp); ...

  3. spring入门(七)【springMVC返回json串】

    现在多数的应用为了提高交互性多使用异步刷新,即在不刷新整个页面的情况下,只刷新局部,局部刷新用得最多就是ajax,ajax和后台进行交互的数据格式使用的最多的是JSON,这里简单描述,在springm ...

  4. java对象与json串互转

    1:java对象与json串转换: java对象—json串: JSONObject JSONStr = JSONObject.fromObject(object); String str = JSO ...

  5. spring 4.x下让http请求返回json串

    当前很多应用已经开始将响应返回为json串,所以基于springframework框架开发的服务端程序,让响应返回json字符串成为了一种常用手段. 这里介绍一下如何在spring-MVC框架下方便快 ...

  6. curl运行json串,代理转发格式

    curl -b 'uin=o0450654733; skey=@tq9xjRvYy' -H "Content-Type: application/json" -X POST -d ...

  7. 使用FastJSON,将对象或数组和JSON串互转

    Fastjson,是阿里巴巴提供的一个Java语言编写的高性能功能完善的JSON库.其开源的下载网址为:https://github.com/AlibabaTech/fastjson. 示例代码如下: ...

  8. 利用QJSON将FDQuery转成JSON串

    服务器要支持Http协议,打算采用Http+JSON的方式来交换数据.一开始考虑使用superobject,因为以前使用比较多,比较熟悉. 代码如下: class function FDQueryTo ...

  9. 怎么解析json串在.net中

    以前知道一种解析json串的方法,觉得有点麻烦.就从别的地方搜到了另一种 string json = vlt.getlist(); JObject jo = JObject.Parse(json); ...

  10. JSON详解以及可以把javabean转换成json串的json-lib应用

    JSON 1. json是什么 它是js提供的一种数据交换格式! 2. json的语法 {}:是对象! 属性名必须使用双引号括起来!单引不行!!! 属性值:null,数值,字符串,数组:使用[]括起来 ...

随机推荐

  1. 删除mysql重复记录的办法

    网上有很多的办法,但是大多数都是通过临时表的办法,其实你是可以用一句简单的sql就可以做到: alter ignore table SOMETABLE add primary key(fields n ...

  2. google code 上传源码

    在使用google code 的时候 做个备份, git clone https://wushuangzilong@code.google.com/p/maplebanana-proxy/ git c ...

  3. iOS设备、Icon、LaunchImage、图片分辨率

    iOS设备 iOS设备的屏幕的大小.分辨率以及比例因数(Scale Factor)[1]. iPhone 设备 宽(inch) 高(inch) 对角线(inch) 逻辑分辨率(point) Scale ...

  4. Spring MVC 3.2 406 Not Acceptable

    Spring MVC 3.2 406 Not Acceptable 这个报错主要是因为SpringMVC配置文件配置问题. 修改步骤如下: 首先,修改spring-mvc.xsd为 spring-mv ...

  5. angularJs 页面筛选标签小功能

    页面html: <div class="bar bar-calm bar-header"> <div class="title">新闻分 ...

  6. php几个比较高级的函数

    1.传递任意数量的函数参数 我们在.NET或者JAVA编程中,一般函数参数个数都是固定的,但是PHP允许你使用任意个数的参数.下面这个示例向你展示了PHP函数的默认参数: 1 2 3 4 5 6 7 ...

  7. 软件测试software testing summarize

    软件测试(英语:software testing),描述一种用来促进鉴定软件的正确性.完整性.安全性和质量的过程.软件测试的经典定义是:在规定的条件下对程序进行操作,以发现程序错误,衡量软件质量,并对 ...

  8. Model Thinking1

    Why Model Reason # 1: Intelligent Citizen of the World Reason # 2: Clearer Thinker Reason # 3: Under ...

  9. namenode启动参数

    namenode启动参数:-Xmx153600m -Xms153600m -Xmn4096m -verbose:gc -Xloggc:$LOG_DIR/namenode.gc.log -XX:Erro ...

  10. uboot移植之环境变量在NandFlash

    一.概述 u-boot环境变量可以设置在Norflash上,也可以在NandFlash上. 倘若环境变量在NorFlash上,再假设S3C2440从NorFlash启动,是能正确从NorFlash上读 ...