• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C# JsonTextReader类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了C#中JsonTextReader的典型用法代码示例。如果您正苦于以下问题:C# JsonTextReader类的具体用法?C# JsonTextReader怎么用?C# JsonTextReader使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



JsonTextReader类属于命名空间,在下文中一共展示了JsonTextReader类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。

示例1: Example

        public void Example()
        {
            #region Usage
            // person.json, has a relative external schema reference 'address.json'
            // --------
            // {
            //   'type': 'object',
            //   'properties': {
            //     'name': {'type':'string'},
            //     'addresses': {
            //       'type': 'array',
            //       'items': {'$ref': 'address.json'}
            //     }
            //   }
            // }
            // --------

            using (StreamReader file = File.OpenText(@"c:\person.json"))
            using (JsonTextReader reader = new JsonTextReader(file))
            {
                JSchemaUrlResolver resolver = new JSchemaUrlResolver();

                JSchema schema = JSchema.Load(reader, new JSchemaReaderSettings
                {
                    Resolver = resolver,
                    // where the schema is being loaded from
                    // referenced 'address.json' schema will be loaded from disk at 'c:\address.json'
                    BaseUri = new Uri(@"c:\person.json")
                });

                // validate JSON
            }
            #endregion
        }
开发者ID:Pondidum,项目名称:Newtonsoft.Json.Schema,代码行数:34,代码来源:JSchemaUrlResolverRelative.cs


示例2: ReadFrom

    public void ReadFrom()
    {
      JObject o = (JObject)JToken.ReadFrom(new JsonTextReader(new StringReader("{'pie':true}")));
      Assert.AreEqual(true, (bool)o["pie"]);

      JArray a = (JArray)JToken.ReadFrom(new JsonTextReader(new StringReader("[1,2,3]")));
      Assert.AreEqual(1, (int)a[0]);
      Assert.AreEqual(2, (int)a[1]);
      Assert.AreEqual(3, (int)a[2]);

      JsonReader reader = new JsonTextReader(new StringReader("{'pie':true}"));
      reader.Read();
      reader.Read();

      JProperty p = (JProperty)JToken.ReadFrom(reader);
      Assert.AreEqual("pie", p.Name);
      Assert.AreEqual(true, (bool)p.Value);

      JConstructor c = (JConstructor)JToken.ReadFrom(new JsonTextReader(new StringReader("new Date(1)")));
      Assert.AreEqual("Date", c.Name);
      Assert.IsTrue(JToken.DeepEquals(new JValue(1), c.Values().ElementAt(0)));

      JValue v;

      v = (JValue)JToken.ReadFrom(new JsonTextReader(new StringReader(@"""stringvalue""")));
      Assert.AreEqual("stringvalue", (string)v);

      v = (JValue)JToken.ReadFrom(new JsonTextReader(new StringReader(@"1")));
      Assert.AreEqual(1, (int)v);

      v = (JValue)JToken.ReadFrom(new JsonTextReader(new StringReader(@"1.1")));
      Assert.AreEqual(1.1, (double)v);
    }
开发者ID:adrianhewitt,项目名称:to-Screenshot-Generator,代码行数:33,代码来源:JTokenTests.cs


示例3: Example

        public void Example()
        {
            #region Usage
            string json = @"{ 'name': 'Admin' }{ 'name': 'Publisher' }";

            IList<Role> roles = new List<Role>();

            JsonTextReader reader = new JsonTextReader(new StringReader(json));
            reader.SupportMultipleContent = true;

            while (true)
            {
                if (!reader.Read())
                    break;

                JsonSerializer serializer = new JsonSerializer();
                Role role = serializer.Deserialize<Role>(reader);

                roles.Add(role);
            }

            foreach (Role role in roles)
            {
                Console.WriteLine(role.Name);
            }

            // Admin
            // Publisher
            #endregion

            Assert.AreEqual(2, roles.Count);
            Assert.AreEqual("Admin", roles[0].Name);
            Assert.AreEqual("Publisher", roles[1].Name);
        }
开发者ID:b-bot-110,项目名称:Newtonsoft.Json,代码行数:34,代码来源:ReadMultipleContentWithJsonReader.cs


示例4: ReadLargeJson

        public static void ReadLargeJson()
        {
            using (var jsonFile = System.IO.File.OpenText("large.json"))
            using (JsonTextReader jsonTextReader = new JsonTextReader(jsonFile))
            {
                JsonSerializer serializer = new JsonSerializer();
                serializer.Deserialize<IList<PerformanceTests.RootObject>>(jsonTextReader);
            }

            Console.WriteLine("Press any key to start deserialization");
            Console.ReadKey();
            Console.WriteLine("Deserializing...");

            Stopwatch sw = new Stopwatch();
            sw.Start();

            for (int i = 0; i < 5; i++)
            {
                using (var jsonFile = System.IO.File.OpenText("large.json"))
                using (JsonTextReader jsonTextReader = new JsonTextReader(jsonFile))
                {
                    JsonSerializer serializer = new JsonSerializer();
                    serializer.Deserialize<IList<PerformanceTests.RootObject>>(jsonTextReader);
                }
            }

            sw.Stop();

            Console.WriteLine("Finished. Total seconds: " + sw.Elapsed.TotalSeconds);
        }
开发者ID:NTUST-PTL,项目名称:PTL-Project,代码行数:30,代码来源:Program.cs


示例5: AssertImport

 private static void AssertImport(object expected, string input)
 {
     JsonTextReader reader = new JsonTextReader(new StringReader(input));
     Type expectedType = expected.GetType();
     object o = JsonImporterStock.Get(expectedType).Import(reader);
     Assert.IsInstanceOfType(expectedType, o);
     Assert.AreEqual(expected, o);
 }
开发者ID:BackupTheBerlios,项目名称:jayrock-svn,代码行数:8,代码来源:TestNumberImporter.cs


示例6: CheckInnerReader

    public void CheckInnerReader()
    {
      string json = "{'name':'James','hobbies':['pie','cake']}";
      JsonReader reader = new JsonTextReader(new StringReader(json));

      JsonValidatingReader validatingReader = new JsonValidatingReader(reader);
      Assert.AreEqual(reader, validatingReader.Reader);
    }
开发者ID:bladefist,项目名称:Newtonsoft.Json,代码行数:8,代码来源:JsonValidatingReaderTests.cs


示例7: AssertImport

 private static void AssertImport(object expected, string input)
 {
     JsonTextReader reader = new JsonTextReader(new StringReader(input));
     Type expectedType = expected.GetType();
     ImportContext context = new ImportContext();
     object o = context.Import(expectedType, reader);
     Assert.IsInstanceOfType(expectedType, o);
     Assert.AreEqual(expected, o);
 }
开发者ID:BackupTheBerlios,项目名称:jayrock-svn,代码行数:9,代码来源:TestNumberImporter.cs


示例8: Load

        public void Load()
        {
            JsonReader reader = new JsonTextReader(new StringReader("new Date(123)"));
            reader.Read();

            JConstructor constructor = JConstructor.Load(reader);
            Assert.AreEqual("Date", constructor.Name);
            Assert.IsTrue(JToken.DeepEquals(new JValue(123), constructor.Values().ElementAt(0)));
        }
开发者ID:JamesNK,项目名称:Newtonsoft.Json,代码行数:9,代码来源:JConstructorTests.cs


示例9: ReaderValidation

        private void ReaderValidation()
        {
            JsonTextReader reader = new JsonTextReader(new StringReader(Json));
            JSchemaValidatingReader validatingReader = new JSchemaValidatingReader(reader);
            validatingReader.Schema = Schema;

            while (validatingReader.Read())
            {
            }
        }
开发者ID:Pondidum,项目名称:Newtonsoft.Json.Schema,代码行数:10,代码来源:PerformanceTests.cs


示例10: AssertImport

 private static void AssertImport(object expected, string input)
 {
     JsonTextReader reader = new JsonTextReader(new StringReader(input));
     GuidImporter importer = new GuidImporter();
     object o = importer.Import(new ImportContext(), reader);
     Assert.IsTrue(reader.EOF, "Reader must be at EOF.");
     if (expected != null)
         Assert.IsInstanceOfType(typeof(Guid), o);
     Assert.AreEqual(expected, o);
 }
开发者ID:BackupTheBerlios,项目名称:jayrock-svn,代码行数:10,代码来源:TestGuidImporter.cs


示例11: Example

    public void Example()
    {
      JObject o1 = JObject.Parse(File.ReadAllText(@"c:\videogames.json"));

      // read JSON directly from a file
      using (StreamReader file = File.OpenText(@"c:\videogames.json"))
      using (JsonTextReader reader = new JsonTextReader(file))
      {
        JObject o2 = (JObject) JToken.ReadFrom(reader);
      }
    }
开发者ID:che3tah7,项目名称:Newtonsoft.Json,代码行数:11,代码来源:ReadJson.cs


示例12: CanReadComma

        public void CanReadComma()
        {
            // arrange
            var reader = new JsonTextReader(new StringReader(","));

            // act
            var token = reader.NextToken();

            // assert
            Assert.Equal(JsonToken.Comma, token);
        }
开发者ID:cosullivan,项目名称:JsonLite,代码行数:11,代码来源:JsonTextReaderFacts.cs


示例13: CanReadDecimal

        public void CanReadDecimal()
        {
            // arrange
            var reader = new JsonTextReader(new StringReader("123.456"));

            // act
            var token = reader.NextToken();

            // assert
            Assert.Equal(new JsonToken(JsonTokenKind.Fractional, "123.456"), token);
        }
开发者ID:cosullivan,项目名称:JsonLite,代码行数:11,代码来源:JsonTextReaderFacts.cs


示例14: CanReadBooleanTrue

        public void CanReadBooleanTrue()
        {
            // arrange
            var reader = new JsonTextReader(new StringReader("true"));

            // act
            var token = reader.NextToken();

            // assert
            Assert.Equal(JsonToken.True, token);
        }
开发者ID:cosullivan,项目名称:JsonLite,代码行数:11,代码来源:JsonTextReaderFacts.cs


示例15: ReadDollarQuoteStringWithTag

        public void ReadDollarQuoteStringWithTag()
        {
            string json = @"{""NameOfStore"":$tag$Forest's Bakery And Cafe$tag$}";

            JsonTextReader jsonTextReader = new JsonTextReader(new StringReader(json));
            jsonTextReader.Read();
            jsonTextReader.Read();
            jsonTextReader.Read();

            Assert.AreEqual(@"Forest's Bakery And Cafe", jsonTextReader.Value);
        }
开发者ID:alanthinker,项目名称:Newtonsoft.Json,代码行数:11,代码来源:DollarQuoteStringTest.cs


示例16: Example

    public void Example()
    {
      // read file into a string and parse JsonSchema from the string
      JsonSchema schema1 = JsonSchema.Parse(File.ReadAllText(@"c:\schema.json"));

      // read JsonSchema directly from a file
      using (StreamReader file = File.OpenText(@"c:\schema.json"))
      using (JsonTextReader reader = new JsonTextReader(file))
      {
        JsonSchema schema2 = JsonSchema.Read(reader);
      }
    }
开发者ID:che3tah7,项目名称:Newtonsoft.Json,代码行数:12,代码来源:LoadJsonSchemaFromFile.cs


示例17: LoadFile

        public void LoadFile()
        {
#region LoadFile
            using (StreamReader file = File.OpenText(@"c:\person.json"))
            using (JsonTextReader reader = new JsonTextReader(file))
            {
                JSchema schema = JSchema.Load(reader);

                // validate JSON
            }
            #endregion
        }
开发者ID:Nangal,项目名称:Newtonsoft.Json.Schema,代码行数:12,代码来源:LoadingSchemas.cs


示例18: CreateReader

        private JSchemaValidatingReader CreateReader(string json, JSchema schema, out IList<SchemaValidationEventArgs> errors)
        {
            JsonReader reader = new JsonTextReader(new StringReader(json));

            List<SchemaValidationEventArgs> localErrors = new List<SchemaValidationEventArgs>();

            JSchemaValidatingReader validatingReader = new JSchemaValidatingReader(reader);
            validatingReader.ValidationEventHandler += (sender, args) => { localErrors.Add(args); };
            validatingReader.Schema = schema;

            errors = localErrors;
            return validatingReader;
        }
开发者ID:Nangal,项目名称:Newtonsoft.Json.Schema,代码行数:13,代码来源:JSchemaValidatingReaderTests.cs


示例19: Example

        public void Example()
        {
            #region Usage
            // read file into a string and parse JSchema from the string
            JSchema schema1 = JSchema.Parse(File.ReadAllText(@"c:\schema.json"));

            // load JSchema directly from a file
            using (StreamReader file = File.OpenText(@"c:\schema.json"))
            using (JsonTextReader reader = new JsonTextReader(file))
            {
                JSchema schema2 = JSchema.Load(reader);
            }
            #endregion
        }
开发者ID:Pondidum,项目名称:Newtonsoft.Json.Schema,代码行数:14,代码来源:LoadJsonSchemaFromFile.cs


示例20: ParseRequest

        private void ParseRequest(string request)
        {
            using (var stringReader = new StringReader(request))
            using (var reader = new JsonTextReader(stringReader))
            {
                reader.DateParseHandling = DateParseHandling.None;

                // Messages parsed as a dictionary with the following
                // parameters:
                //
                //   'r': Request type: 0, 1 or 2
                //   'a': Association ID when the request type is 1 or 2
                //   't': Message type
                //   'p': Payload

                if (!reader.Read())
                    throw new HttpException("Invalid request");

                if (reader.TokenType == JsonToken.StartObject)
                {
                    ProcessMessageRequest(reader);
                }
                else if (reader.TokenType == JsonToken.StartArray)
                {
                    ReadToken(reader, JsonToken.String);

                    string action = (string)reader.Value;

                    ReadToken(reader, JsonToken.EndArray);

                    switch (action)
                    {
                        case "close":
                            _client.Dispose();
                            break;

                        default:
                            throw new HttpException("Invalid request");
                    }
                }
                else
                {
                    throw new HttpException("Invalid request");
                }

                if (reader.Read())
                    throw new HttpException("Invalid request");

            }
        }
开发者ID:gmt-europe,项目名称:ProtoChannel,代码行数:50,代码来源:ChannelUpstreamRequest.cs



注:本文中的JsonTextReader类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# JsonTextWriter类代码示例发布时间:2022-05-24
下一篇:
C# JsonServiceClient类代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap