Please tell me some sort of example of parsing JSON strings under the OXIDE. Already all head broke, anything sensible and did not work. Sorry for the Google translation
Solved Parsing JSON from webrequest?
Discussion in 'Rust Development' started by TRJfl@sh, Sep 15, 2016.
-
Are you trying to read a config, a data file or the result of a web request?
-
Sorry, I forgot. Webrequest
-
Here's an example I quickly stole from @Mughisi
Code:// Reference: Newtonsoft.Jsonusing Newtonsoft.Json; using System.Linq;using Oxide.Core; using Oxide.Core.Libraries;namespace Oxide.Plugins { [Info("Test", "Mughisi", 1.0)] class Tests : RustPlugin { public class GetOwnedGamesResponse { [JsonProperty("response")] public Content Response { get; set; } public class Content { [JsonProperty("game_count")] public int GameCount { get; set; } [JsonProperty("games")] public Game[] Games { get; set; } } public class Game { [JsonProperty("appid")] public int Appid { get; set; } [JsonProperty("playtime_forever")] public int PlaytimeForever { get; set; } [JsonProperty("playtime_2weeks")] public int? Playtime2weeks { get; set; } } } private void OnServerInitialized() { var id = 0; var key = ""; var url = $"http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key={key}&steamid={id}&format=json"; Interface.GetMod().GetLibrary<WebRequests>("WebRequests").EnqueueGet(url, WebrequestCallback, this); } void WebrequestCallback(int code, string response) { switch (code) { case 200: var json = JsonConvert.DeserializeObject<GetOwnedGamesResponse>(response); var gametime = json.Response.Games.Single(x => x.Appid == 252490).PlaytimeForever; Puts($"The player played {gametime} hours of rust!"); break; case 401: Puts("Webrequest failed, invalid Steam API key."); break; case 404: case 503: Puts("Webrequest failed. Steam API unavailable."); break; default: Puts($"Webrequest failed. Error code {code}."); break; } } } }
-
Interesting. But if one of the elements is an array? I stutter that is parsing a nested array
-
-
I apologize, overlooked. Thank you I will try!
-
The magic reference to Newtonsoft.Json should no longer be required
(That is the `// Reference: Newtonsoft.Json` on top)
-
So, getting sorted out and how much is it to loop through?
JSON for example
Code:{ "error":false, "goods":[{ "good":"VIP", "total":"1", "good_id":"2"}, { "good":"Same", "total":"1", "good_id":"1"} ], "services":[{ "good":"VIP", "total":"1", "good_id":"2"}, { "good":"Same", "total":"1", "good_id":"1"} ] }
-
Something like this should do the trick:
Code:using Newtonsoft.Json;using Oxide.Core; using Oxide.Core.Libraries;namespace Oxide.Plugins { [Info("JsonExample", "Mughisi", 1.0)] class JsonExample : RustPlugin { public class JsonResponse { [JsonProperty("error")] public bool Error { get; set; } [JsonProperty("goods")] public Good[] Goods { get; set; } [JsonProperty("services")] public Service[] Services { get; set; } public class Good { [JsonProperty("good")] public string Name { get; set; } [JsonProperty("total")] public string Total { get; set; } [JsonProperty("good_id")] public string GoodId { get; set; } } public class Service { [JsonProperty("good")] public string Name { get; set; } [JsonProperty("total")] public string Total { get; set; } [JsonProperty("good_id")] public string GoodId { get; set; } } } private void OnServerInitialized() { var url = $"http://www.yoursite.ext/callback.php?id=123"; Interface.GetMod().GetLibrary<WebRequests>("WebRequests").EnqueueGet(url, WebrequestCallback, this); } void WebrequestCallback(int code, string response) { switch (code) { case 200: var json = JsonConvert.DeserializeObject<JsonResponse>(response); Puts($"Error: {json.Error}"); Puts("Goods:"); foreach(var good in json.Goods) Puts($"{good.GoodId} - {good.Name} - {good.Total}"); Puts("Services:"); foreach(var service in json.Services) Puts($"{service.GoodId} - {service.Name} - {service.Total}"); break; default: Puts($"Webrequest failed. Error code {code}."); break; } } } }
-
Thank you exactly what you need! Good thing there are people to help the newcomers. To be honest I have a small problem with c# it would be easier to write in PHP
-
Or how to get the value of the parameter. I was looking for, honestly...Last edited by a moderator: Oct 4, 2016