码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • c# 把 json 字符串解析成 json 对象(小例子)


    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Text;
    5. using Kingdee.BOS.JSON;
    6. using Newtonsoft.Json;
    7. using System;
    8. using Newtonsoft.Json;
    9. using Newtonsoft.Json.Linq;
    10. namespace C_Sharp_XueXi
    11. {
    12. class Program
    13. {
    14. static void Main(string[] args)
    15. {
    16. // JSON字符串
    17. string json = @"
    18. {
    19. ""Name"": ""Alice"",
    20. ""Age"": 25,
    21. ""City"": ""New York"",
    22. ""Pets"": [
    23. {
    24. ""Name"": ""Fluffy"",
    25. ""Type"": ""Cat""
    26. },
    27. {
    28. ""Name"": ""Fido"",
    29. ""Type"": ""Dog""
    30. }
    31. ],
    32. ""Friends"": [
    33. {
    34. ""Name"": ""Bob"",
    35. ""Age"": 30,
    36. ""City"": ""Chicago""
    37. },
    38. {
    39. ""Name"": ""Eve"",
    40. ""Age"": 28,
    41. ""City"": ""San Francisco""
    42. }
    43. ]
    44. }";
    45. // 解析JSON字符串
    46. JObject jsonObj = JObject.Parse(json);
    47. // 获取特定字段的值
    48. string name = (string)jsonObj["Name"];
    49. int age = (int)jsonObj["Age"];
    50. string city = (string)jsonObj["City"];
    51. // 获取数组字段的值
    52. JArray pets = (JArray)jsonObj["Pets"];
    53. foreach (JObject pet in pets)
    54. {
    55. string petName = (string)pet["Name"];
    56. string petType = (string)pet["Type"];
    57. Console.WriteLine("Pet: Name = {0}, Type = {1}", petName, petType);
    58. }
    59. // 获取嵌套对象字段的值
    60. JArray friends = (JArray)jsonObj["Friends"];
    61. foreach (JObject friend in friends)
    62. {
    63. string friendName = (string)friend["Name"];
    64. int friendAge = (int)friend["Age"];
    65. string friendCity = (string)friend["City"];
    66. Console.WriteLine("Friend: Name = {0}, Age = {1}, City = {2}", friendName, friendAge, friendCity);
    67. }
    68. }
    69. }
    70. }

  • 相关阅读:
    Java-泛型基础
    桌面云架构讲解(VDI、IDV、VOI/TCI、RDS)
    【计算机毕业设计】基于SpringBoot+Vue大学生心理健康管理系统的开发与实现
    Simulink模型加密共享
    竞赛选题 深度学习人体跌倒检测 -yolo 机器视觉 opencv python
    [Leetcode] 0058. 最后一个单词的长度
    .NET CLR介绍
    羊大师提示,羊奶都有哪些惊人功效?
    【In-Context Learning】Meta-learning via Language Model In-context Tuning
    听GPT 讲Rust源代码--library/std(7)
  • 原文地址:https://blog.csdn.net/xiaoyuanyuan2009/article/details/134443094
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号