win10 uwp json

简介: 原文:win10 uwp json 本文讲的是关于在uwp使用json的简单使用,json应用很多,因为我只是写简单使用,说的东西可能不对或者不符合每个人的预期。
原文: win10 uwp json

本文讲的是关于在uwp使用json的简单使用,json应用很多,因为我只是写简单使用,说的东西可能不对或者不符合每个人的预期。如果觉得我有讲的不对的,就多多包含,或者直接关掉这篇文章,但是请勿生气或者发怒吐槽,可以在我博客评论 http://blog.csdn.net/lindexi_gd

现在很多应用都是使用json

如果我们拿到一段json,想要把它变为我们C艹艹可以用的,我们需要先对json的类进行转换,其实很简单,我们在复制一段json

不需要我们对这json打,因为我们有vs,在我们的编辑,可以看到

这里写图片描述

我们复制完一段json,然后点击粘贴,就好了,自动生成

如果我们拿到一段json

{
  "results": [{
    "location": {
      "id": "WX4FBXXFKE4F",
      "name": "北京",
      "country": "CN",
      "path": "北京,北京,中国",
      "timezone": "Asia/Shanghai",
      "timezone_offset": "+08:00"
    },
    "daily": [{      
      "date": "2015-09-20",           
      "text_day": "多云",              
      "code_day": "4",                 
      "text_night": "晴",              
      "code_night": "0",              
      "high": "26",                
      "low": "17",                 
      "precip": "0",                 
      "wind_direction": "",           
      "wind_direction_degree": "255",   
      "wind_speed": "9.66",            
      "wind_scale": ""                 
    }, {
      "date": "2015-09-21",
      "text_day": "晴",
      "code_day": "0",
      "text_night": "晴",
      "code_night": "0",
      "high": "27",
      "low": "17",
      "precip": "0",
      "wind_direction": "",
      "wind_direction_degree": "157",
      "wind_speed": "17.7",
      "wind_scale": "3"
    }, {
    }],
    "last_update": "2015-09-20T18:00:00+08:00" 
  }]
}

我们弄个新的类,粘贴

    public class Thinw
    {

        public class Rootobject
        {
            public Result[] results { get; set; }
        }

        public class Result
        {
            public Location location { get; set; }
            public Daily[] daily { get; set; }
            public DateTime last_update { get; set; }
        }

        public class Location
        {
            public string id { get; set; }
            public string name { get; set; }
            public string country { get; set; }
            public string path { get; set; }
            public string timezone { get; set; }
            public string timezone_offset { get; set; }
        }

        public class Daily
        {
            public string date { get; set; }
            public string text_day { get; set; }
            public string code_day { get; set; }
            public string text_night { get; set; }
            public string code_night { get; set; }
            public string high { get; set; }
            public string low { get; set; }
            public string precip { get; set; }
            public string wind_direction { get; set; }
            public string wind_direction_degree { get; set; }
            public string wind_speed { get; set; }
            public string wind_scale { get; set; }
        }
    }

很快我们就得到了我们需要序列的类

接着我们使用Nuget

这里写图片描述

当然我还加上九幽的插件,九幽有几个插件可以获得我们应用数据,我们启动我们关闭,还有广告很好用

我们使用Nuget主要下载Newtonsoft.Json

我们转序可以使用

        public void Unencoding(string str)
        {
            var json = JsonSerializer.Create();
            Rootobject thinw = json.Deserialize<Rootobject>(new JsonTextReader(new StringReader(str)));
        }

如果我们需要把我们的类转为json

            var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("data", CreationCollisionOption.ReplaceExisting);
            using (TextWriter stream = new StreamWriter(await file.OpenStreamForWriteAsync()))
            {
                json.Serialize(stream, thinw);
            }

这样把我们的类保存在文件

如果觉得我做的简单,想要使用微软的Windows.Data.Json ,其实使用Newtosoft的才好

如果使用Windows.Data.Json

JsonArray root = JsonValue.Parse(jsonString).GetArray();  
for (uint i = 0; i < root.Count; i++) {  
    string name1 = root.GetObjectAt(i).GetNamedString("name");  
    string description1 = root.GetObjectAt(i).GetNamedString("description");  
    string link1 = root.GetObjectAt(i).GetNamedString("link");  
    string cat1 = root.GetObjectAt(i).GetNamedString("cat");  
    string image1 = root.GetObjectAt(i).GetNamedString("image");  
    var chan = new RootObject {  
        name = name1, cat = cat1, description = description1, image = image1, link = link1  
    };  
    obs_channels.Add(chan);  
}); 

如果属性多,基本上很多人会容易就打错,当然,可以先实例一个RootObject,然后使用新关键字,name去得到实例属性名称

当然在我们使用Json会遇到一些属性我们不要的,那么如何json忽略属性,其实很简单,在Newtosoft可以在属性加[JsonIgnore],因为这些比较乱,所以也不打算在这里说。

首先是我们的类,

  public class Thine
  {
      public string Property{set;get;}
      public string Ignore{set;get;}
  }

我们要把Property包含,但是不包含Ignore,简单的

  public class Thine
  {
      public string Property{set;get;}
      [JsonIgnore]
      public string Ignore{set;get;}
  }

但是有时候我们要包含很少,基本都是不包含的,那么如何只用包含,其实我们可以在类加[JsonObject(MemberSerialization.OptIn)]看到了OptIn,其实OptOut就是不包含一些,OptIn就是包含一些


  [JsonObject(MemberSerialization.OptIn)]
  public class Thine
  {
      [JsonProperty]
      public string Property{set;get;}
      public string Ignore{set;get;}
  }

知识共享许可协议
本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接:http://blog.csdn.net/lindexi_gd ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系

目录
相关文章
|
8月前
|
机器学习/深度学习 JSON 前端开发
一篇文章讲明白dropzone上传文件
一篇文章讲明白dropzone上传文件
131 0
|
9月前
|
自然语言处理 机器人 API
手把手做一个公众号GPT智能客服(七)GPT 接入微信机器人
手把手做一个公众号GPT智能客服(七)GPT 接入微信机器人
245 1
|
自然语言处理 JavaScript 前端开发
LayUI之动态选项卡Tab&iframe使用
LayUI之动态选项卡Tab&iframe使用
462 0
|
前端开发
CSS - 移动端 1px 像素边框问题的解决方案(Border.css)
CSS - 移动端 1px 像素边框问题的解决方案(Border.css)
293 0
CSS - 移动端 1px 像素边框问题的解决方案(Border.css)
ArcGIS 栅格数据已加载后的获取
原文 http://www.cnblogs.com/zoe-j/archive/2012/02/16/2354037.html 简单记一下,最近开始做Arcgis engine的开发, 已经通过了toolbar 加载的栅格数据,之后我想处理该数据。
853 0
|
8月前
结构体\判断日期是否合法(代码分步解析)
结构体\判断日期是否合法(代码分步解析)
52 1
npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree
npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve dependency tree
|
机器人
Day39——62.不同路径 63. 不同路径 II
Day39——62.不同路径 63. 不同路径 II
94 0
|
3天前
|
人工智能 自然语言处理 Shell
深度评测 | 仅用3分钟,百炼调用满血版 Deepseek-r1 API,百万Token免费用,简直不要太爽。
仅用3分钟,百炼调用满血版Deepseek-r1 API,享受百万免费Token。阿里云提供零门槛、快速部署的解决方案,支持云控制台和Cloud Shell两种方式,操作简便。Deepseek-r1满血版在推理能力上表现出色,尤其擅长数学、代码和自然语言处理任务,使用过程中无卡顿,体验丝滑。结合Chatbox工具,用户可轻松掌控模型,提升工作效率。阿里云大模型服务平台百炼不仅速度快,还确保数据安全,值得信赖。
144367 24
深度评测 | 仅用3分钟,百炼调用满血版 Deepseek-r1 API,百万Token免费用,简直不要太爽。

热门文章

最新文章