Related
so i'm getting a json file from the web into my program that reads something like this:
{
"10": {
"appid": 10,
"name": "Counter-Strike",
"developer": "Valve",
"publisher": "Valve",
"score_rank": "",
"positive": 183964,
"negative": 4782,
"userscore": 0,
"owners": "10,000,000 .. 20,000,000",
"average_forever": 11228,
"average_2weeks": 289,
"median_forever": 210,
"median_2weeks": 114,
"price": "999",
"initialprice": "999",
"discount": "0",
"ccu": 13567
},
"20": {
"appid": 20,
"name": "Team Fortress Classic",
"developer": "Valve",
"publisher": "Valve",
"score_rank": "",
"positive": 5223,
"negative": 871,
"userscore": 0,
"owners": "2,000,000 .. 5,000,000",
"average_forever": 522,
"average_2weeks": 0,
"median_forever": 20,
"median_2weeks": 0,
"price": "499",
"initialprice": "499",
"discount": "0",
"ccu": 93
},
"30": {
"appid": 30,
"name": "Day of Defeat",
"developer": "Valve",
"publisher": "Valve",
"score_rank": "",
"positive": 4866,
"negative": 543,
"userscore": 0,
"owners": "5,000,000 .. 10,000,000",
"average_forever": 2191,
"average_2weeks": 343,
"median_forever": 24,
"median_2weeks": 343,
"price": "499",
"initialprice": "499",
"discount": "0",
"ccu": 130
},
"40": {
"appid": 40,
"name": "Deathmatch Classic",
"developer": "Valve",
"publisher": "Valve",
"score_rank": "",
"positive": 1789,
"negative": 400,
"userscore": 0,
"owners": "5,000,000 .. 10,000,000",
"average_forever": 297,
"average_2weeks": 0,
"median_forever": 8,
"median_2weeks": 0,
"price": "499",
"initialprice": "499",
"discount": "0",
"ccu": 6
}
I'm importing it has a string how can I get an enumerable or list where I get all the tokens as (Jtokens) so "IEnumerable<JToken> or List<JToken>" like ["10", "40", "60"...]
This is what my code looks like right now:
string json = webClient.DownloadString("https://api.steampowered.com/ISteamApps/GetAppList/v2/");
tokens = JObject.Parse(json).Children();
//token = JObject.Parse(json).SelectToken("applist.apps");
for (int i = 0; i < tokens.Count(); i++)
{
int currentID = (int)tokens.ElementAt(i).SelectToken("appid");
if (SteamApps.BIsSubscribedApp(new AppId_t((uint)currentID)))
{
threads.Add(new Thread(new ParameterizedThreadStart(AddToDictionary)));
threads.Last().Start(new stats(i, currentID, threads.Last()));
}
}
But this isn't working at all and I can't read any values..
There are oodles of ways to do this and variations. However, this is simple enough. The premise is Parse -> Select First -> Target property by name
var results = JToken.Parse(input)
// Select past the dictionary
.Select(x => x.First())
// project property values in to a value tuple
.Select(x => (Appid: x["appid"], Name: x["name"]))
.ToArray();
foreach (var item in results)
Console.WriteLine(item);
Output
(10, Counter-Strike)
(20, Team Fortress Classic)
(30, Day of Defeat)
(40, Deathmatch Classic)
by selecting the token u want from the list of objects that was deserialised from the json string using system.linq
newlist = deserList.Select(x => x.Jtoken);
Could someone help me how I can get the strings after the English > srt
I only wanna grab this strings:
/20200623/c1/51/1777469aba625657d69b3cbaefed1387.srt?qd_uid=0&qd_tm=1594275239930&qd_tvid=16385349500&qyid=f0f6a8fe625d5f29422131ad7c466dbf&lid=3
"_name": "Vietnamese",
"_sort": 6,
"mver": "101",
"lid": 23,
"xml": "/20200623/c0/72/e00ed17c479f2700194837e1d3901e32.xml?qd_uid=0&qd_tm=1594275239930&qd_tvid=16385349500&qyid=f0f6a8fe625d5f29422131ad7c466dbf&lid=23",
"srt": "/20200623/c0/72/41619b14d41606def042d6d267b72de2.srt?qd_uid=0&qd_tm=1594275239930&qd_tvid=16385349500&qyid=f0f6a8fe625d5f29422131ad7c466dbf&lid=23",
"_selected": false,
"pre": 0,
"ie": 0,
"uuid": "b908453e2869285ef8a1f75599ae85d9",
"_limited": 1,
"ssv": 1,
"webvtt": "/20200623/c0/72/cbc73fb5b7da5282d0697f265e08c87c.vtt?qd_uid=0&qd_tm=1594275239930&qd_tvid=16385349500&qyid=f0f6a8fe625d5f29422131ad7c466dbf&lid=23",
"ss": 0
},
{
"_name": "English",
"_sort": 1,
"mver": "101",
"lid": 3,
"xml": "/20200623/c1/51/445603c545708bf234430da417a9469f.xml?qd_uid=0&qd_tm=1594275239930&qd_tvid=16385349500&qyid=f0f6a8fe625d5f29422131ad7c466dbf&lid=3",
"srt": "/20200623/c1/51/1777469aba625657d69b3cbaefed1387.srt?qd_uid=0&qd_tm=1594275239930&qd_tvid=16385349500&qyid=f0f6a8fe625d5f29422131ad7c466dbf&lid=3",
"_selected": true,
"pre": 0,
"ie": 0,
"uuid": "c802506ad202e27680b8096ccc18f36d",
"_limited": 1,
"ssv": 1,
"webvtt": "/20200623/c1/51/da547e3c9d716037190173eabfbf4eaf.vtt?qd_uid=0&qd_tm=1594275239930&qd_tvid=16385349500&qyid=f0f6a8fe625d5f29422131ad7c466dbf&lid=3",
"ss": 0
I finally figure it out to get the English link only
string json = richTextBox1.Text;
JObject jObject = JObject.Parse(json);
var subtitle = jObject["data"]["program"]["stl"][8]["srt"]; //[8] is the English
output.Text = subtitle.ToString();
Consider you have Order and OrderDetails. I'm trying to filter order details for the specific order.
For instance if you have document something like this:
{
"orders": [
{
"id": 1,
"date": "02.04.2020 ...",
"user": {
"name": "xx",
"surname": "yy"
},
"orderDetails": [
{
"id": 1,
"productId": 5,
"quantity": 1,
"state": 3
},
{
"id": 2,
"productId": 3,
"quantity": 4,
"state": 3
},
{
"id": 3,
"productId": 4,
"quantity": 12,
"state": 2
},
{
"id": 4,
"productId": 7,
"quantity": 8,
"state": 2
},
{
"id": 5,
"productId": 12,
"quantity": 9,
"state": 3
}
]
},
{
"id": 2,
"date": "01.04.2020 ...",
"user": {
"name": "xx",
"surname": "yy"
},
"orderDetails": [
{
"id": 6,
"productId": 5,
"quantity": 1,
"state": 3
},
{
"id": 7,
"productId": 3,
"quantity": 4,
"state": 3
},
{
"id": 8,
"productId": 4,
"quantity": 12,
"state": 2
}
]
}
}
What I'm trying to do is first filtering by order and then state of an order detail. I have a code like this but it always brings correct order with all orderDetails. Seems like it doesn't care about equal filter for orderDetails.
Actually it's working but not filtering. Because I only have 3 types of state(enum) and int values are 1,2,3. Query brings nothing if I give 4.
var builder = Builders<Order>.Filter;
var filter = builderk.And(builder.Eq("_id", ObjectId.Parse(elementid)), builder.Eq("orderDetails.state", 3));
var result = _mongoRepository.FindByFilter(filter).ToList();
I also tried AnyEq and something like that filters but didn't work.
I will be very happy if anyone can help me.
Thanks.
You can use aggregation operation for operations such as filter sorting in detail records.
If we continue through the sample, you must first create a filter for your master data.
var builderMaster = Builders<Order>.Filter;
var filterMaster = builderMaster.Eq("_id", ObjectId.Parse(elementid));
Then you need to create a different filter for the details.
Important: You must use the BsonDocument type when creating detail filters. Because you cannot give a specific type when you filter the details.
var builderDetail = Builders<BsonDocument>.Filter;
var filterDetail = builderDetail.Eq("orderDetails.state", 3);
Then you can start typing the query.
var list = _mongoRepository.Aggregate()
.Match(filterMaster)
.Unwind("orderDetails")// Name the details.
.Match(filterDetail)
.Sort(sort)// optionally
.Skip(skip) // optionally
.Limit(limit) // optionally
.ToList();
It will give you a list of BsonDocument with the given parameters. After that, you have to map with your own detail class.
var resultList = new List<OrderDetails>();
foreach (var master in list)
{
var masterObj = BsonSerializer.Deserialize<dynamic>(master);
foreach (var item in masterObj)
{
if (item.Key == "orderDetails")
{
var mapper = new MapperConfiguration(cfg => { }).CreateMapper();
var retItem = mapper.Map<OrderDetails>(item.Value);
resultList.Add(retItem);
}
}
}
I have a JSON data like bellow
[{
"CurrencyDenomination_JSON": "[{\"PK_MasterCurrencyDenomID\":1,\"CurrencyDenomination\":2000,\"NoofCurrency\":2,\"Total\":\"4000.00\",\"IsNote\":true},{\"PK_MasterCurrencyDenomID\":2,\"CurrencyDenomination\":500,\"NoofCurrency\":2,\"Total\":\"1000.00\",\"IsNote\":true}]"
}, {
"CurrencyDenomination_JSON": "[{\"PK_MasterCurrencyDenomID\":1,\"CurrencyDenomination\":2000,\"NoofCurrency\":5,\"Total\":\"10000.00\",\"IsNote\":true},{\"PK_MasterCurrencyDenomID\":2,\"CurrencyDenomination\":500,\"NoofCurrency\":2,\"Total\":\"1000.00\",\"IsNote\":true}]"
}, {
"CurrencyDenomination_JSON": "[{\"PK_MasterCurrencyDenomID\":1,\"CurrencyDenomination\":2000,\"NoofCurrency\":5,\"Total\":\"10000.00\",\"IsNote\":true},{\"PK_MasterCurrencyDenomID\":2,\"CurrencyDenomination\":500,\"NoofCurrency\":5,\"Total\":\"2500.00\",\"IsNote\":true}]"
}]
and I want to extract value from it and expect bellow data
[{
"PK_MasterCurrencyDenomID": 1,
"CurrencyDenomination": 2000,
"NoofCurrency": 2,
"Total": "4000.00",
"IsNote": true
}, {
"PK_MasterCurrencyDenomID": 2,
"CurrencyDenomination": 500,
"NoofCurrency": 2,
"Total": "1000.00",
"IsNote": true
}, {
"PK_MasterCurrencyDenomID": 3,
"CurrencyDenomination": 200,
"NoofCurrency": 2,
"Total": "400.00",
"IsNote": true
}, {
"PK_MasterCurrencyDenomID": 4,
"CurrencyDenomination": 100,
"NoofCurrency": 2,
"Total": "200.00",
"IsNote": true
}, {
"PK_MasterCurrencyDenomID": 5,
"CurrencyDenomination": 50,
"NoofCurrency": 2,
"Total": "100.00",
"IsNote": true
}, {
"PK_MasterCurrencyDenomID": 6,
"CurrencyDenomination": 20,
"NoofCurrency": 2,
"Total": "40.00",
"IsNote": true
}]
to do that I write bellow code ,and think this is not right way to do that there must be some smart way to do that .Please suggest me a better alternative.
JArray jsonArray = JArray.Parse(json);
List<MasterCurrencyDenomination> d = new List<MasterCurrencyDenomination>();
string strjson = string.Empty;
foreach (var item in jsonArray.Children())
{
strjson+= item["CurrencyDenomination_JSON"].ToString().Replace("[", "").Replace("]", ",");
}
d = JsonConvert.DeserializeObject<List<MasterCurrencyDenomination>>("["+strjson+"]");
If you observe the Json, it is evident that it is an Array of Type which contains a single String Property CurrencyDenomination_JSON. The Value of CurrencyDenomination_JSON is a JSON string.
What you need to do is, fetch these JSON strings (represented as IEnumerable<string>), retrieve JObject from them by parsing each of them and Serialize the collection.
var currencyArray = JArray.Parse(json).Children<JObject>()
.SelectMany(x=>x.Properties().Select(c=>c.Value.Value<string>()));
var result = JsonConvert.SerializeObject(currencyArray.SelectMany(x=>JArray.Parse(x)));
I think You can use the following code to do this.
List<MasterCurrencyDenomination> items = new List<MasterCurrencyDenomination>();
JToken.Parse(myJson).ToList().ForEach(x =>
{
items.AddRange(JsonConvert.DeserializeObject<List<MasterCurrencyDenomination>>(x.SelectToken("CurrencyDenomination_JSON").Value<string>()));
});
OR:
List<MasterCurrencyDenomination> items2 = new List<MasterCurrencyDenomination>();
foreach (var test in JToken.Parse(myJson))
{
items2.AddRange(JsonConvert.DeserializeObject<List<MasterCurrencyDenomination>>(test.SelectToken("CurrencyDenomination_JSON").Value<string>()));
}
I am attempting to deserialize a JSON response from a webserver which I have no control over. As you can see below the resulting object is returned as a JSONObject. The issue with this is that the key of each object is unique to every player. As far as I can tell I am required to create a separate class for each unique JSONObject to deserialize the string properly which simply would not work considering the key for each object is unique to each created account that is returned from the webserver. I want to know if it is possible to deserialize the JSONObject "players" as a JSONArray or if I am overthinking this a bit too much. The ammount of players returned in the list also varies for up to a maximum of 32.
{
"players": {
"930153636": {
"name": "ur_moms_cookies",
"tag": "",
"rank": 62,
"score": 1332,
"kills": 9,
"deaths": 2,
"squad": 1,
"role": 1
},
"250721735": {
"name": "PyRobotic",
"tag": "",
"rank": 5,
"score": 1786,
"kills": 8,
"deaths": 4,
"squad": 2,
"role": 1
},
"1035406266": {
"name": "ShrapnalFire",
"tag": "GaGu",
"rank": 60,
"score": 1455,
"kills": 8,
"deaths": 7,
"squad": 1,
"role": 1
},
"239767512": {
"name": "NeoEllis",
"tag": "",
"rank": 77,
"score": 3244,
"kills": 20,
"deaths": 6,
"squad": 3,
"role": 1
},
"936400786": {
"name": "antiares51",
"tag": "",
"rank": 140,
"score": 4720,
"kills": 17,
"deaths": 2,
"squad": 2,
"role": 1
},
"355364783": {
"name": "BombKat11",
"tag": "",
"rank": 16,
"score": 1347,
"kills": 9,
"deaths": 5,
"squad": 4,
"role": 1
},
"803046096": {
"name": "thejoedude",
"tag": "FSJ",
"rank": 66,
"score": 1294,
"kills": 8,
"deaths": 8,
"squad": 4,
"role": 1
},
"788221305": {
"name": "Tiyou33",
"tag": "",
"rank": 45,
"score": 1927,
"kills": 4,
"deaths": 5,
"squad": 2,
"role": 1
},
"353394766": {
"name": "DKOfTFC",
"tag": "RSPN",
"rank": 119,
"score": 1895,
"kills": 9,
"deaths": 4,
"squad": 4,
"role": 1
},
"370847289": {
"name": "shohet10",
"tag": "",
"rank": 40,
"score": 1570,
"kills": 7,
"deaths": 7,
"squad": 6,
"role": 1
},
"206872615": {
"name": "me94132",
"tag": "",
"rank": 60,
"score": 2045,
"kills": 7,
"deaths": 4,
"squad": 3,
"role": 1
},
"332317870": {
"name": "frehgv",
"tag": "",
"rank": 48,
"score": 713,
"kills": 3,
"deaths": 4,
"squad": 4,
"role": 1
},
"1144758913": {
"name": "MudCruncher_78",
"tag": "",
"rank": 61,
"score": 1822,
"kills": 7,
"deaths": 5,
"squad": 6,
"role": 1
},
"603412175": {
"name": "UsedTissue",
"tag": "",
"rank": 119,
"score": 1551,
"kills": 10,
"deaths": 1,
"squad": 1,
"role": 1
},
"1655466910": {
"name": "JackTheRipp3r2",
"tag": "",
"rank": 27,
"score": 797,
"kills": 2,
"deaths": 3,
"squad": 8,
"role": 1
},
"869745436": {
"name": "Negibou",
"tag": "",
"rank": 139,
"score": 4792,
"kills": 14,
"deaths": 4,
"squad": 2,
"role": 1
},
"447652633": {
"name": "RufusTheRabbit",
"tag": "",
"rank": 91,
"score": 1468,
"kills": 7,
"deaths": 4,
"squad": 4,
"role": 1
},
"352650707": {
"name": "DJ1101",
"tag": "",
"rank": 28,
"score": 847,
"kills": 5,
"deaths": 6,
"squad": 3,
"role": 1
},
"855246326": {
"name": "-Frigadier-",
"tag": "",
"rank": 109,
"score": 667,
"kills": 1,
"deaths": 6,
"squad": 1,
"role": 1
},
"255855183": {
"name": "Asakajim",
"tag": "",
"rank": 6,
"score": 514,
"kills": 0,
"deaths": 8,
"squad": 8,
"role": 1
},
"248119620": {
"name": "Kokovec",
"tag": "FCM",
"rank": 121,
"score": 891,
"kills": 5,
"deaths": 3,
"squad": 8,
"role": 1
},
"348016421": {
"name": "pandaymd",
"tag": "",
"rank": 122,
"score": 1595,
"kills": 6,
"deaths": 9,
"squad": 2,
"role": 1
},
"250545778": {
"name": "Vulkkann",
"tag": "",
"rank": 40,
"score": 2323,
"kills": 14,
"deaths": 8,
"squad": 3,
"role": 1
},
"949941984": {
"name": "WarchiefBigzb",
"tag": "",
"rank": 70,
"score": 1013,
"kills": 5,
"deaths": 3,
"squad": 9,
"role": 1
},
"268773188": {
"name": "Brutuka",
"tag": "",
"rank": 48,
"score": 952,
"kills": 5,
"deaths": 4,
"squad": 5,
"role": 1
},
"801259600": {
"name": "gpc4567",
"tag": "",
"rank": 109,
"score": 1710,
"kills": 11,
"deaths": 5,
"squad": 3,
"role": 1
},
"338093557": {
"name": "Zelios86",
"tag": "",
"rank": 140,
"score": 1400,
"kills": 7,
"deaths": 4,
"squad": 8,
"role": 1
},
"364705773": {
"name": "Fleischwolfx",
"tag": "",
"rank": 121,
"score": 1863,
"kills": 13,
"deaths": 2,
"squad": 5,
"role": 1
},
"173524306": {
"name": "BlackDynamite901",
"tag": "",
"rank": 75,
"score": 583,
"kills": 4,
"deaths": 2,
"squad": 9,
"role": 1
},
"339527854": {
"name": "Xixus",
"tag": "",
"rank": 21,
"score": 0,
"kills": 0,
"deaths": 0,
"squad": 1,
"role": 1
},
"1667184018": {
"name": "BckBone71",
"tag": "",
"rank": 35,
"score": 98,
"kills": 1,
"deaths": 1,
"squad": 5,
"role": 1
}
}
}
Thus far I have tried a few variations of something along the lines of the below code snippet which of course does not work since the players object isn't an array.
public class Team
{
public int faction;
[JsonProperty("players")]
public List<Player> players { get; set; }
}
public class Player
{
public string name;
public string tag;
public int rank;
}
Try something like this:
var players = new List<Player>();
dynamic dObject = JObject.Parse(JSON);
foreach (var property in dObject.players) {
var player = property.Value;
var playerModel = new Player {
Name = player.name,
Tag = player.tag,
Rank = player.rank
};
players.Add(playerModel);
}
Where JSON is your Json object. In this case, I just made it a string literal to test your object. This is using NewtonSoft.Json for the parsing.
Once you get the player, you should have access to all of its properties and not have to worry about the unique player ID.
Here is the .NET fiddle: https://dotnetfiddle.net/9iOT5w.