This article is about the simple use of JSON in the UWP, the JSON app is a lot, because I just write simple to use, said things may be wrong or not meet everyone's expectations. If you feel that I have something wrong, please include it, or just turn off the article, but don't be angry or cross-spit, you can comment on my blog http://blog.csdn.net/lindexi_gd
Many applications are now using JSON
If we get a JSON and want to change it to our C 艹 艹, we need to convert the JSON class first, it's very simple, we're copying a JSON
Don't need us to hit on this JSON because we have vs, in our editor, can see
We copy a JSON, then click Paste, just fine, auto-generated
If we get a JSON
{ "Results":[{ " Location":{"ID": " wx4fbxxfke4f", "name": "Beijing", "country": "CN" /c8>, "path": "Beijing, Beijing, China", "timezone": "Asia/shanghai", "ti Mezone_offset": " +08:00 " }, "Daily":[{ "Date":"2015-09-20", "Text_day":"Sunny", "Code_day":"4", "Text_night":"Fine", "Code_night":"0", " High":"a", " Low":" the", "Precip":"0", "wind_direction":"", "Wind_direction_degree":"255", "Wind_speed":"9.66", "Wind_scale":"" }, { "Date":"2015-09-21", "Text_day":"Fine", "Code_day":"0", "Text_night":"Fine", "Code_night":"0", " High":" the", " Low":" the", "Precip":"0", "wind_direction":"", "Wind_direction_degree":"157", "Wind_speed":"17.7", "Wind_scale":"3" }, { }], "last_update":"2015-09-20t18:00:00+08:00" }]}
Let's get a new class, paste
Public classTHINW { Public classRootobject { PublicResult[] Results {Get;Set; } } Public classResult { PublicLocation Location {Get;Set; } PublicDaily[] Daily {Get;Set; } PublicDateTime Last_update {Get;Set; } } Public classLocation { Public stringID {Get;Set; } Public stringName {Get;Set; } Public stringCountry {Get;Set; } Public stringPath {Get;Set; } Public stringTimeZone {Get;Set; } Public stringTimezone_offset {Get;Set; } } Public classDaily { Public stringDate {Get;Set; } Public stringText_day {Get;Set; } Public stringCode_day {Get;Set; } Public stringText_night {Get;Set; } Public stringCode_night {Get;Set; } Public stringHigh {Get;Set; } Public stringLow {Get;Set; } Public stringPrecip {Get;Set; } Public stringwind_direction {Get;Set; } Public stringWind_direction_degree {Get;Set; } Public stringWind_speed {Get;Set; } Public stringWind_scale {Get;Set; } } }
Soon we get the classes we need to sequence.
Then we use NuGet
Of course I also add nine of the plug-in, nine have a few plug-ins can get our application data, we start our close, and advertising is very useful
We use nuget primarily to download Newtonsoft.json
We can use the transfer
publicvoidUnencoding(string str) { var json = JsonSerializer.Create(); Rootobject thinw = json.Deserialize<Rootobject>(new JsonTextReader(new StringReader(str))); }
If we need to convert our class to JSON,
var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("data", CreationCollisionOption.ReplaceExisting); using (TextWriter stream = new StreamWriter(await file.OpenStreamForWriteAsync())) { json.Serialize(stream, thinw); }
This saves our class in the file
If think I do simple, want to use Microsoft's Windows.Data.Json, actually use Newtosoft's only good
If you use 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); });
If there are many properties, basically a lot of people will easily be wrong, of course, you can first instantiate a rootobject, and then use the new keyword, name to get the instance property name
Of course, when we use JSON we will encounter some properties we do not, then how JSON ignores properties, in fact very simple, in the Newtosoft can be added in the attribute [Jsonignore], because these are more chaotic, so also do not intend to say here.
The first is our class,
publicclass Thine { publicstring Property{set;get;} publicstring Ignore{set;get;} }
We're going to include the property, but it doesn't contain ignore, it's simple.
publicclass Thine { publicstring Property{set;get;} [JsonIgnore] publicstring Ignore{set;get;} }
But sometimes we have to contain very little, basically is not included, then how to only include, in fact, we can see in the class [JsonObject(MemberSerialization.OptIn)]
optin, in fact, OptOut is not contain some, OptIn is contains some
[JsonObject(MemberSerialization.OptIn)] publicclass Thine { [JsonProperty] publicstring Property{set;get;} publicstring Ignore{set;get;} }
This work is licensed under the Creative Commons Attribution-NonCommercial use-Share 4.0 International license agreement in the same way. Welcome to reprint, use, republish, but be sure to keep the article Attribution Lindesi (including Link: http://blog.csdn.net/lindexi_gd), not for commercial purposes, based on the modified works of this article must be issued with the same license. If you have any questions, please contact me.
WIN10 UWP JSON