Code Monkey home page Code Monkey logo

jsondict's Introduction

JsonDict

一个非常简单的.net JSON解析器, 把JSON内容解析到字典.

支持.net8 native AOT

支持使用Path来访问JSON内容

A very simple. net JSON parser that parses JSON content into a dictionary. Support native AOT. Support using Path to access JSON content

string jsonString = "{\"name\":\"John\",\"age\":30,\"city\":\"New York\",\"grades\":[90, 85, 92]}";

var jo = JsonDict.Parse(jsonString);

// 使用路径访问
Console.WriteLine("使用路径访问:");
Console.WriteLine($"Name: {jo.GetValue("name")}");
Console.WriteLine($"City: {jo.GetValue("city")}");
Console.WriteLine($"First Grade: {jo.GetValue("grades.0")}");

复杂JSON访问

string jsonString3 = @"{
    ""glossary"": {
        ""title"": ""example glossary"",
		""GlossDiv"": {
            ""title"": ""S"",
			""GlossList"": {
                ""GlossEntry"": {
                    ""ID"": ""SGML"",
					""SortAs"": ""SGML"",
					""GlossTerm"": ""Standard Generalized Markup Language"",
					""Acronym"": ""SGML"",
					""Abbrev"": ""ISO 8879:1986"",
					""GlossDef"": {
                        ""para"": ""A meta-markup language, used to create markup languages such as DocBook."",
						""GlossSeeAlso"": [""GML"", ""XML""]
                    },
					""GlossSee"": ""markup""
                }
            }
        }
    }
}";
var jo3 = JsonDict.Parse(jsonString3);
Console.WriteLine(jo3.GetValue("glossary.title"));
Console.WriteLine(jo3.GetValue("glossary.GlossDiv.GlossList.GlossEntry.GlossDef.para"));
Console.WriteLine(jo3.GetValue("glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSeeAlso.0"));

字典和数组的访问

var arr = jo3.GetValue("glossary.GlossDiv.GlossList.GlossEntry.GlossDef.GlossSeeAlso").Array;
foreach (var item in arr)
{
    Console.WriteLine(item);
}
foreach (var item in jo3.Keys)
{
    Console.WriteLine($"{item}\t\t{jo3.GetValue(item)}");
}

jsondict's People

Contributors

zzandww avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

nclnclncl

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.