In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to understand the ASP.NET universal JSON parser". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Concept introduction
Let's start with some examples of Json. Note that the following concepts are defined by myself. You can refer to the model design of TYPE in. Net.
If there is any dispute, you are welcome to discuss it!
1. The simplest:
{"total": 0}
Total is a value, a value is a numeric value, which is equal to 0
two。 Be more complicated
{"total": 0, "data": {"377149574": 1}}
Total is the value, and data is the object, which contains the value of "377149574", which is equal to 1.
3. The most complex
{"total": 0, "data": {"377149574": [{"cid": "377149574"}]}}
Total is the value, data is the object, and 377149574 is the array, which contains a series of objects, such as {"cid": "377149574"}.
With the above concepts, you can design a general json model.
Universal JSON source code:
The copy code is as follows:
Using System
Using System.Collections.Generic
Using System.Text
Namespace Pixysoft.Json
{
Public class CommonJsonModelAnalyzer
{
Protected string _ GetKey (string rawjson)
{
If (string.IsNullOrEmpty (rawjson))
Return rawjson
Rawjson = rawjson.Trim ()
String [] jsons = rawjson.Split (new char [] {':'})
If (jsons.Length
< 2) return rawjson; return jsons[0].Replace("\"", "").Trim(); } protected string _GetValue(string rawjson) { if (string.IsNullOrEmpty(rawjson)) return rawjson; rawjson = rawjson.Trim(); string[] jsons = rawjson.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries); if (jsons.Length < 2) return rawjson; StringBuilder builder = new StringBuilder(); for (int i = 1; i < jsons.Length; i++) { builder.Append(jsons[i]); builder.Append(":"); } if (builder.Length >0)
Builder.Remove (builder.Length-1,1)
String value = builder.ToString ()
If (value.StartsWith ("\"))
Value = value.Substring (1)
If (value.EndsWith ("\"))
Value = value.Substring (0, value.Length-1)
Return value
}
Protected List _ GetCollection (string rawjson)
{
/ / [{}, {}]
List list = new List ()
If (string.IsNullOrEmpty (rawjson))
Return list
Rawjson = rawjson.Trim ()
StringBuilder builder = new StringBuilder ()
Int nestlevel =-1
Int mnestlevel =-1
For (int I = 0; I
< rawjson.Length; i++) { if (i == 0) continue; else if (i == rawjson.Length - 1) continue; char jsonchar = rawjson[i]; if (jsonchar == '{') { nestlevel++; } if (jsonchar == '}') { nestlevel--; } if (jsonchar == '[') { mnestlevel++; } if (jsonchar == ']') { mnestlevel--; } if (jsonchar == ',' && nestlevel == -1 && mnestlevel == -1) { list.Add(builder.ToString()); builder = new StringBuilder(); } else { builder.Append(jsonchar); } } if (builder.Length >0)
List.Add (builder.ToString ())
Return list
}
}
}
The copy code is as follows:
Using System
Using System.Collections.Generic
Using System.Text
Namespace Pixysoft.Json
{
Public class CommonJsonModel: CommonJsonModelAnalyzer
{
Private string rawjson
Private bool isValue = false
Private bool isModel = false
Private bool isCollection = false
Internal CommonJsonModel (string rawjson)
{
This.rawjson = rawjson
If (string.IsNullOrEmpty (rawjson))
Throw new Exception ("missing rawjson")
Rawjson = rawjson.Trim ()
If (rawjson.StartsWith ("{"))
{
IsModel = true
}
Else if (rawjson.StartsWith ("["))
{
IsCollection = true
}
Else
{
IsValue = true
}
}
Public string Rawjson
{
Get {return rawjson;}
}
Public bool IsValue ()
{
Return isValue
}
Public bool IsValue (string key)
{
If (! isModel)
Return false
If (string.IsNullOrEmpty (key))
Return false
Foreach (string subjson in base._GetCollection (this.rawjson))
{
CommonJsonModel model = new CommonJsonModel (subjson)
If (! model.IsValue ()
Continue
If (model.Key = = key)
{
CommonJsonModel submodel = new CommonJsonModel (model.Value)
Return submodel.IsValue ()
}
}
Return false
}
Public bool IsModel ()
{
Return isModel
}
Public bool IsModel (string key)
{
If (! isModel)
Return false
If (string.IsNullOrEmpty (key))
Return false
Foreach (string subjson in base._GetCollection (this.rawjson))
{
CommonJsonModel model = new CommonJsonModel (subjson)
If (! model.IsValue ()
Continue
If (model.Key = = key)
{
CommonJsonModel submodel = new CommonJsonModel (model.Value)
Return submodel.IsModel ()
}
}
Return false
}
Public bool IsCollection ()
{
Return isCollection
}
Public bool IsCollection (string key)
{
If (! isModel)
Return false
If (string.IsNullOrEmpty (key))
Return false
Foreach (string subjson in base._GetCollection (this.rawjson))
{
CommonJsonModel model = new CommonJsonModel (subjson)
If (! model.IsValue ()
Continue
If (model.Key = = key)
{
CommonJsonModel submodel = new CommonJsonModel (model.Value)
Return submodel.IsCollection ()
}
}
Return false
}
/ / /
/ / return the owned key when the model is an object
/ / /
/ / /
Public List GetKeys ()
{
If (! isModel)
Return null
List list = new List ()
Foreach (string subjson in base._GetCollection (this.rawjson))
{
String key = new CommonJsonModel (subjson) .Key
If (! string.IsNullOrEmpty (key))
List.Add (key)
}
Return list
}
/ / /
/ / when the model is an object and key corresponds to a value, the value corresponding to key is returned.
/ / /
/ / /
/ / /
Public string GetValue (string key)
{
If (! isModel)
Return null
If (string.IsNullOrEmpty (key))
Return null
Foreach (string subjson in base._GetCollection (this.rawjson))
{
CommonJsonModel model = new CommonJsonModel (subjson)
If (! model.IsValue ()
Continue
If (model.Key = = key)
Return model.Value
}
Return null
}
/ / /
/ Model is an object, key corresponds to an object, and returns the object corresponding to key
/ / /
/ / /
/ / /
Public CommonJsonModel GetModel (string key)
{
If (! isModel)
Return null
If (string.IsNullOrEmpty (key))
Return null
Foreach (string subjson in base._GetCollection (this.rawjson))
{
CommonJsonModel model = new CommonJsonModel (subjson)
If (! model.IsValue ()
Continue
If (model.Key = = key)
{
CommonJsonModel submodel = new CommonJsonModel (model.Value)
If (! submodel.IsModel ()
Return null
Else
Return submodel
}
}
Return null
}
/ / /
/ Model is an object, key corresponds to a collection, and returns a collection
/ / /
/ / /
/ / /
Public CommonJsonModel GetCollection (string key)
{
If (! isModel)
Return null
If (string.IsNullOrEmpty (key))
Return null
Foreach (string subjson in base._GetCollection (this.rawjson))
{
CommonJsonModel model = new CommonJsonModel (subjson)
If (! model.IsValue ()
Continue
If (model.Key = = key)
{
CommonJsonModel submodel = new CommonJsonModel (model.Value)
If (! submodel.IsCollection ()
Return null
Else
Return submodel
}
}
Return null
}
/ / /
/ / the model is a collection and returns itself
/ / /
/ / /
Public List GetCollection ()
{
List list = new List ()
If (IsValue ())
Return list
Foreach (string subjson in base._GetCollection (rawjson))
{
List.Add (new CommonJsonModel (subjson))
}
Return list
}
/ / /
/ / return key when the model is a value object
/ / /
Private string Key
{
Get
{
If (IsValue ())
Return base._GetKey (rawjson)
Return null
}
}
/ / /
/ / return value when the model is a value object
/ / /
Private string Value
{
Get
{
If (! IsValue ()
Return null
Return base._GetValue (rawjson)
}
}
}
}
Usage
Public CommonJsonModel DeSerialize (string json)
{
Return new CommonJsonModel (json)
}
Super simple, as long as new a general object, put the json string on it.
For the three examples above, I give three ways to use them:
{"total": 0}
CommonJsonModel model = DeSerialize (json)
Model.GetValue ("total") / / return 0
{"total": 0, "data": {"377149574": 1}}
CommonJsonModel model = DeSerialize (json)
Model.GetModel ("data") .GetValue ("377149574") / / return 1
{"total": 0, "data": {"377149574": [{"cid": "377149574"}]}}
CommonJsonModel model = DeSerialize (json)
Model.GetCollection ("377149574"). GetCollection () [0] .GetValue ("cid") / / return 377149574
This is a little complicated.
1. First, 377149574 represents a collection, so use model.GetCollection ("377149574") to fetch the collection.
two。 Secondly, this collection contains a lot of objects, so use GetColllection () to take them out
3. Take the first [0] in the List of these objects, indicating that the object ": {" cid ":" 377149574 "} is taken, and then use GetValue (" cid ") to take out the value of the object.
This is the end of "how to understand the ASP.NET Universal JSON Parser". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.