随笔 - 432  文章 - 0  评论 - 15  阅读 - 63万

c#操作json数据使用newtonsoft.json

开源项目提供的一个读取示例

复制代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Newtonsoft.Json.Tests.Documentation.Samples.Json
{
  public class ReadJsonWithJsonTextReader
  {
    public void Example()
    {
      #region Usage
      string json = @"{
         'CPU': 'Intel',
         'PSU': '500W',
         'Drives': [
           'DVD read/writer'
           /*(broken)*/,
           '500 gigabyte hard drive',
           '200 gigabype hard drive'
         ]
      }";

      JsonTextReader reader = new JsonTextReader(new StringReader(json));
      while (reader.Read())
      {
        if (reader.Value != null)
          Console.WriteLine("Token: {0}, Value: {1}", reader.TokenType, reader.Value);
        else
          Console.WriteLine("Token: {0}", reader.TokenType);
      }

      // Token: StartObject
      // Token: PropertyName, Value: CPU
      // Token: String, Value: Intel
      // Token: PropertyName, Value: PSU
      // Token: String, Value: 500W
      // Token: PropertyName, Value: Drives
      // Token: StartArray
      // Token: String, Value: DVD read/writer
      // Token: Comment, Value: (broken)
      // Token: String, Value: 500 gigabyte hard drive
      // Token: String, Value: 200 gigabype hard drive
      // Token: EndArray
      // Token: EndObject
      #endregion
    }
  }
}
复制代码

json 读取

复制代码
//json读取示例
        public void jsonreadsample() 
        {
            string jsonstr = "{\"Name\" : \"Jack\", \"Age\" : 34, \"Colleagues\" : [{\"Name\" : \"Tom\" , \"Age\":44},{\"Name\" : \"Abel\",\"Age\":29}] }";
            //将json转换为JObject
            JObject jo = JObject.Parse(jsonstr);
            JToken ageToken=jo["Name"]; //获取该员工的姓名
            Console.WriteLine(ageToken.ToString());

            //获取该员工同事所有姓名(读取json数组)
            var names=from staff in jo["Colleagues"].Children() select (string)staff["Name"];
            foreach (var name in names)
                Console.WriteLine(name);
        }
复制代码

url编码

复制代码
//URL encode
        public String UrlEncoded(string str)
        {
            StringBuilder sb = new StringBuilder();
            byte[] byStr = System.Text.Encoding.UTF8.GetBytes(str); //默认是System.Text.Encoding.Default.GetBytes(str)
            for (int i = 0; i < byStr.Length; i++)
            {
                sb.Append(@"%" + Convert.ToString(byStr[i], 16).ToUpper());
            }

            return (sb.ToString());
        }
复制代码

 

posted on   狼来了  阅读(10586)  评论(0编辑  收藏  举报
编辑推荐:
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示