c# multi level json - c#
I am trying to make a simple program that can automate price checking using data from json, however I haven't used json before and I'm not quite sure what I need to do get get the desired result.
Effectively I'm trying to convert this PHP into c#. (http://www.sourcecodester.com/tutorials/php/8042/api-json-parsing-php.html)
<?php
$src = file_get_contents('https://www.g2a.com/marketplace/product/auctions/?id=256');
$json = json_decode($src, true);
foreach ($json as $k=>$v) {
if (is_array($v)) {
foreach($v as $key=>$value) {
if (is_array($value)) {
foreach($value as $arKey => $arValue) {
if ($arKey == 'p') {
echo $arValue . '<br/>';
}
}
}
}
}
}
?>
I've tried a few things such as JsonConvert.DeserializeObject(webJson) and JObject.Parse(webJson), but I'm just not sure where I should be looking or how to go about doing it.
I have the first part:
internal static string GetWebContent(string #ID)
{
var wc = new WebClient();
string response = wc.DownloadString(#"https://www.g2a.com/marketplace/product/auctions/?id=" + #ID);
return response;
}
A response looks like this: (https://www.g2a.com/marketplace/product/auctions/?id=27446)
{"a":{"k_709942":{"c":"pl","tr":3451,"f":"\u00a324.71","fb":"\u00a327.18","ci":"565784","cname":"Marketplace User","p":"24.7098173","pb":"27.18431394","ep":"35.15","epb":"38.67","a":"709942","it":"game","t":"1","sl":"0","l":null,"lt":null,"x":0,"v":"all","so":0,"r":99},"k_763218":{"c":"pl","tr":1120,"f":"\u00a324.74","fb":"\u00a327.22","ci":"6533797","cname":"User #f36726dcd","p":"24.7449664","pb":"27.21946304","ep":"35.20","epb":"38.72","a":"763218","it":"game","t":"0","sl":"0","l":null,"lt":"0","x":0,"v":"all","so":0,"r":92},"k_799750":{"c":"pl","tr":559,"f":"\u00a324.78","fb":"\u00a327.26","ci":"6115711","cname":"Marketplace User","p":"24.7801155","pb":"27.26164196","ep":"35.25","epb":"38.78","a":"799750","it":"game","t":null,"sl":"0","l":null,"lt":null,"x":0,"v":"retail","so":0,"r":98},"k_722082":{"c":"gb","tr":49066,"f":"\u00a324.96","fb":"\u00a327.45","ci":"1047917","cname":"Marketplace User","p":"24.955861","pb":"27.4514471","ep":"35.50","epb":"39.05","a":"722082","it":"game","t":"1","sl":"0","l":null,"lt":"0","x":0,"v":"all","so":0,"r":98},"k_718113":{"c":"pl","tr":5852,"f":"\u00a324.96","fb":"\u00a327.45","ci":"226876","cname":"Marketplace User","p":"24.955861","pb":"27.4514471","ep":"35.50","epb":"39.05","a":"718113","it":"game","t":"1","sl":"0","l":null,"lt":"0","x":0,"v":"all","so":0,"r":98},"k_739155":{"c":"pl","tr":1208,"f":"\u00a325.43","fb":"\u00a327.98","ci":"6540559","cname":"User #1f948a6a66249","p":"25.43316468854","pb":"27.976481157394","ep":"35.30","epb":"38.83","a":"739155","it":"game","t":"0","sl":"0","l":null,"lt":"0","x":0,"v":"all","so":0,"r":89},"k_795049":{"c":"pl","tr":50420,"f":"\u00a325.86","fb":"\u00a328.45","ci":"2498689","cname":"Marketplace User","p":"25.86270778","pb":"28.44968154","ep":"36.79","epb":"40.47","a":"795049","it":"game","t":"1","sl":"0","l":null,"lt":"0","x":0,"v":"all","so":0,"r":99},"k_816132":{"c":"pl","tr":22,"f":"\u00a326.00","fb":"\u00a328.60","ci":"6208533","cname":"Marketplace User","p":"25.99627436","pb":"28.59730776","ep":"36.98","epb":"40.68","a":"816132","it":"game","t":"0","sl":"0","l":null,"lt":"0","x":0,"v":"retail","so":0,"r":0},"k_809925":{"c":"pl","tr":81021,"f":"\u00a326.00","fb":"\u00a328.60","ci":"407513","cname":"Marketplace User","p":"26.00330418","pb":"28.60433758","ep":"36.99","epb":"40.69","a":"809925","it":"game","t":"1","sl":"0","l":null,"lt":"0","x":0,"v":"retail","so":0,"r":100},"k_815898":{"c":"cl","tr":1,"f":"\u00a326.01","fb":"\u00a328.61","ci":"7524623","cname":"Marketplace User","p":"26.010334","pb":"28.6113674","ep":"37.00","epb":"40.70","a":"815898","it":"game","t":null,"sl":"0","l":null,"lt":null,"x":0,"v":"retail","so":0,"r":0},"k_711901":{"c":"pl","tr":12194,"f":"\u00a326.51","fb":"\u00a329.16","ci":"286793","cname":"Marketplace User","p":"26.506689203722","pb":"29.158078610346","ep":"36.79","epb":"40.47","a":"711901","it":"game","t":"1","sl":"0","l":null,"lt":null,"x":0,"v":"all","so":0,"r":99},"k_748710":{"c":"pt","tr":66460,"f":"\u00a326.65","fb":"\u00a329.32","ci":"440288","cname":"Marketplace User","p":"26.650786454082","pb":"29.316585585742","ep":"36.99","epb":"40.69","a":"748710","it":"game","t":"1","sl":"0","l":null,"lt":"0","x":0,"v":"all","so":0,"r":100},"k_709464":{"c":"pl","tr":121341,"f":"\u00a327.02","fb":"\u00a329.72","ci":"1072530","cname":"User #3285e5f8dfcb2","p":"27.0182344425","pb":"29.72005788675","ep":"37.50","epb":"41.25","a":"709464","it":"game","t":"1","sl":"0","l":null,"lt":"0","x":0,"v":"retail","so":0,"r":100},"k_709805":{"c":"pl","tr":11708,"f":"\u00a328.09","fb":"\u00a330.90","ci":"1043113","cname":"User #ca1965d0354a","p":"28.091758957682","pb":"30.901655339702","ep":"38.99","epb":"42.89","a":"709805","it":"game","t":"1","sl":"0","l":null,"lt":null,"x":0,"v":"retail","so":0,"r":100},"k_725839":{"c":"es","tr":1,"f":"\u00a331.99","fb":"\u00a335.18","ci":"7023396","cname":"Marketplace User","p":"31.985681","pb":"35.1842491","ep":"45.50","epb":"50.05","a":"725839","it":"game","t":null,"sl":"0","l":null,"lt":null,"x":0,"v":"retail","so":0,"r":0},"k_0":{"f":"\u00a332.33","fb":"\u00a335.56","p":"32.33014218","pb":"35.563156398","ep":"45.99","epb":"50.59","a":0,"c":"","rc":"","r":"","tr":0,"t":1,"so":0,"x":0,"n":"G2A.com"}},"w":0}
Any help with will greatly appreciated.
Thanks in advace
So Json is a way for two languages to pass objects to each other. It's just a way to encode an object, so the first part would be to create an object that matches the Json encoding. I was unable to see your example, so I will give you one of my own.
{"Aggregates": [{"ColumnName": "Some Value", "AggregateFunction": "Some Other Value"}], "GroupBy": true}
I would then create a class like this one.
public class JsonAggregates
{
public List<Aggregate> Aggregates { get; set; }
public string GroupBy { get; set; }
}
public class Aggregate
{
public string ColumnName {get; set;}
public string AggregateFunction {get; set;}
}
You can then encode the Json to this new data type with the following code.
using (Stream s = GenerateStreamFromString(json))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(JsonAggregates));
JsonAggregates aggregate = (JsonAggregates)serializer.ReadObject(s);
}
Related
Parse Json_encoded array from PHP in C# UWP
I developing a UWP app using C#, this conect a PHP WebService, this web service return a array encoded using json_encode function, i cant parse this json string in my UWP app, please help 1: PHP CODE $aCli[]= array("CAT"=>"OK","MSG"=>"SESION-OK","EXTRA"=>array("ID"=>"$spID","NOM"=>"$spNom")); echo json_encode($aCli); 2: The result using postman is: [{"CAT":"OK","MSG":"SESION-OK","EXTRA":{"ID":"3","NOM":"CHARLS"}}] 3. The result using Async Task from C# is: "[{\"CAT\":\"OK\",\"MSG\":\"SESION-OK\",\"EXTRA\":{\"ID\":\"3\",\"NOM\":\"CHARLS\"}}]" 4. How to deserialize this string?, i am trying it using using Windows.Data.Json; 5. This is the code using in this time sJSON= await IniciarSesion();//this use async class to connect with webservice JsonObject objJson = JsonObject.Parse(sJSON);//error is raised in this line /*Json String is invalid*/ sCat = objJson["CAT"].GetString(); sMsg = objJson["MSG"].GetString();
Important comment above: your JSON is a representation of an array, not an object. IF you can use Newtonsoft (JSON.Net), here's one way, with dependency on JSON.Net, not Windows.Data.Json. Trivial sample only, improve as necessary (null checks, etc.) using System; using Newtonsoft.Json.Linq; public class Program { public static void Main() { var str = "[{\"CAT\":\"OK\",\"MSG\":\"SESION-OK\",\"EXTRA\":{\"ID\":\"3\",\"NOM\":\"CHARLS\"}}]"; var j = JArray.Parse(str); var token = j[0]; //using dynamic to simplify sample, create/use your own type var obj = token.ToObject<dynamic>(); Console.WriteLine(obj.CAT); Console.WriteLine(obj.MSG); Console.WriteLine(obj.EXTRA); Console.WriteLine(obj.EXTRA.ID); Console.WriteLine(obj.EXTRA.NOM); } } Hth..
You could use Newtonsoft to deserialize JSON string directly. For your requirement, you need to make the data model fist. [{"CAT":"OK","MSG":"SESION-OK","EXTRA":{"ID":"3","NOM":"CHARLS"}}] public class Pet { public string Cat { get; set; } public string Msg { get; set; } public Extra Extra { get; set; } } public class Extra { public string Id { get; set; } public string Nom { get; set; } } Usage private void Button_Click(object sender, RoutedEventArgs e) { var str = "[{\"CAT\":\"OK\",\"MSG\":\"SESION-OK\",\"EXTRA\":{\"ID\":\"3\",\"NOM\":\"CHARLS\"}}]"; var items = JsonConvert.DeserializeObject<List<Pet>>(str); } For more you could refer Serializing and Deserializing JSON documentation.
The problem is with the brackets ' [] ' at the beginning and end of the string, You should trim them like: ' YOUR_JSON_STRING = YOUR_JSON_STRING.trim('[',']'); and then parse the json string below: ' dynamic result = JsonConvert.DeserializeObject(YOUR_JSON_STRING)'
Windows Console Applications VS2105 C# - Using JSON Data
I have at times made it my life's work to avoid API's at all costs (that's a debate for another day) but the time is arriving for this to change, a few months ago I got started on creating an API for my applications, and thanks to this very website, it's worked a charm. So now I'm creating a simple Windows Console Application, it should do nothing more than get API Data then submit to a database for the primary application to use at a later date. So far so good or so I thought. This is what I came up with: static void Main(string[] args) { string pair = "xxx/xxx"; string apiUrl = "http://someURL" + pair; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(apiUrl); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader readerStream = new StreamReader(responseStream, System.Text.Encoding.GetEncoding("utf-8")); string json = readerStream.ReadToEnd(); readerStream.Close(); var jo = JObject.Parse(json); Console.WriteLine("Pair : " + (string)jo["pair"]); Console.WriteLine("Open : " + (string)jo["openPrice"]); Console.WriteLine("Close : " + (string)jo["closePrice"]); Console.WriteLine("Vol : " + (string)jo["vol"]); Console.ReadKey(); } Now this works FANTASTIC for the primary source, but when I change the source (eventually it will be multi source) it fails to work. After some investigation, it seems the slightly different responses are the culprit. API Return looks like this { "ip":"1.1.1.1","country_code":"AU","country_name":"Australia", "region_code":"VIC","region_name":"Victoria","city":"Research", "zip_code":"3095","time_zone":"Australia/Melbourne","latitude":-37.7, "longitude":145.1833,"metro_code":0 } The return for an alternative source looks like this { "success":true,"message":"", "result":[ {"MarketName":"BITCNY-BTC","High":8000.00000001,"Low":7000.00000000, "Volume":0.02672075, "Last":7000.00000000,"BaseVolume":213.34995000, "TimeStamp":"2017-02-09T08:38:22.62","Bid":7000.00000001,"Ask":9999.99999999, "OpenBuyOrders":14, "OpenSellOrders":20,"PrevDay":8000.00000001,"Created":"2015-12-11T06:31:40.653" } ] } As we can see the second return is structured differently, and for this feed I've been unable to work this out, clearly my code works, kind of, and clearly it doesn't. I've looked about on the net and am still no closer to a solution, partly I don't know what I'm really asking and secondly google only wants to talk webAPI. If there is someone who can point me in the right direction, I don't want the work done for me per say that solves nothing, I have got to learn to do this one way or another.
As mentioned in the comments, your strings are unrelated. You won't be able to perform the same actions on both JSON strings because they're different. Your best bet is to work with Deserialization to objects, which is converting a JSON string into an object and working with properties instead of JObject literals. Putting your first JSON string into Json2Csharp.com, this class structure was produced: public class RootObject { public string ip { get; set; } public string country_code { get; set; } public string country_name { get; set; } public string region_code { get; set; } public string region_name { get; set; } public string city { get; set; } public string zip_code { get; set; } public string time_zone { get; set; } public double latitude { get; set; } public double longitude { get; set; } public int metro_code { get; set; } } You could then make some adjustments to your code to extract the HTTP request functionality, assuming that you'll only be working with JSON strings in this instance, and then simply deserialized the JSON to an object. An example of a WebRequest() method would be: private static string WebRequest(string UrlToQuery) { using(WebClient client = new WebClient) { return client.DownloadString(UrlToQuery); } } (Make sure you catch any errors and deal with unexpected responses, I'll leave that up to you!) You can then call this method like so: string JsonResponse = WebRequest(apiUrl); and deserialize to an object of type RootObject like so: RootObject deserializedString = JsonConvert.Deserialize<RootObject>(JsonResponse); You'll then be able to perform something like: Console.WriteLine($"Country name: {deserializedString.country_name}"); Which will print the value of the country_name property of your deserializedString object.
RESTAPI body parameters are passed null when calling in c#
I am trying to call a rest api method from c#. Problem is for all content types it passes null to body parameter.I shared my code below.Apart from this code I have tried to write body parameter to request as stream.It didn't work either. I have also tried 'application/x-www-form-urlencoded' as content type. Calling rest api method from c# sample: string token = Server.UrlEncode("v0WE/49uN1/voNwVA1Mb0MiMrMHjFunE2KgH3keKlIqei3b77BzTmsk9OIREken1hO9guP3qd4ipCBQeBO4jiQ=="); string url = "http://localhost:2323/api/Applications/StartProcess?token=" + token; string data = #"{""ProcessParameters"": [{ ""Name"":""flowStarter"",""Value"": ""Waffles"" }], ""Process"": ""RESTAPISUB""}"; System.Net.Http.HttpClient client = new System.Net.Http.HttpClient(); client.BaseAddress = new System.Uri(url); byte[] cred = UTF8Encoding.UTF8.GetBytes("username:password"); client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(cred)); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); System.Net.Http.HttpContent content = new StringContent(data, UTF8Encoding.UTF8, "application/json"); HttpResponseMessage messge = client.PostAsync(url, content).Result; string description = string.Empty; if (messge.IsSuccessStatusCode) { string result = messge.Content.ReadAsStringAsync().Result; description = result; } Rest api Method: [HttpPost] [ActionName("StartProcess")] public int StartProcess([FromUri]string token,[FromBody]WorkflowStartParameters parameters) { try { LoginInformation info = CasheProcesses.ReadCashe(token); eBAWSAPI api = Service.GetWSService(); WorkflowProcess proc = api.StartProcess(info.Id, info.Password, info.ImpersonateUserId, info.Language, parameters); return proc.ProcessId; } catch (Exception ex) { throw new Exception("An error occured when starting process,exception detail:" + ex); } } WorkflowStartParameters class structure: public class WorkflowStartParameters { public WorkflowParameter[] ProcessParameters; public string Process { get; set; } } public class WorkflowParameter { public string Name { get; set; } public string Value { get; set; } } I have searched this problem a lot. It seems as a very common problem. I just found this solution working properly, passing request parameter to rest api method and reading body parameter from there. But it is not a valid solution for me. If you have any idea,feel free to share. Thanks, Zehra
I don´t know if it can solve your problem, but let me try. I guess you don´t have to utilize Server.UrlEncode in your call, but: Dim myUri As New Uri(Token) And I guess you must not encode also your username and password - try pass them as string.
Your problem appear to be here: public class WorkflowStartParameters { public WorkflowParameter[] ProcessParameters; <---- needs get/set public string Process { get; set; } } This needs to be a public property to serialize properly. Currently you have it set up as a public field. Just add { get; set; } and give that a try. I would also look into serializing with Newtonsoft.Json to ensure your object is properly serialized. Trying to do it with escape strings will be messing the more data you are sending. By the way there can be issues sometimes serializing arrays, I would change that to : public List<WorkflowParameter> ProcessParameters{get;set;}
Finally I have achieved to send filled out data to server. It was about serialization problem. But it didn't work with json serialization before send data. I have added DataContract attribute to my class and it works properly. Unfortunately still I couldn't figure out this when I make ajax calls from java script it works without DataContract attribute but if I call it in c# it needs DataContract attribute. If someone share the information about this I would appreciate! I am sharing new class structure, everything else but this still same: [Serializable] [DataContract] public class WorkflowParameter { [DataMember] public string Name { get; set; } [DataMember] public string Value { get; set; } } [Serializable] [DataContract] public class WorkflowStartParameters { [DataMember] public WorkflowParameter[] ProcessParameters { get; set; } [DataMember] public string Process { get; set; } }
How do I deserialize this JSON array (C#)
I am struggling with a subject that has a lot of variants, but I can't seem to find one that works for me, and I think it's because of the way that my JSON array is. I'm not an expert in C# or JSON, but I already manage to "almost" get this to work. I need to get hand with the class that the JSON will deserialize to. When I run the code I dont get an error, just a nulls in the xKisokData var. The JSON data that I am getting. Their are these two different ones. "{\"Event\": \"sConnection\",\"data[device]\": \"fb16f550-2ef1-11e5-afe9-ff37129acbf4\",\"data[mode]\": \"customer\",\"data[starttime]\": \"2015-07-22T16:07:42.030Z\",\"data[endtime]\": \"\"}" "{\"Event\": \"Log\",\"data[id]\": \"2015-07-22T16:07:23.063Z\",\"data[messages][0][source]\": \"server\",\"data[messages][0][message]\": \"Server is listening on port 1553\"}" The code I have so far: // Read in our Stream into a string... StreamReader reader = new StreamReader(JSONdataStream); string JSONdata = reader.ReadToEnd(); JavaScriptSerializer jss = new JavaScriptSerializer(); wsKisokData[] xKisokData = jss.Deserialize<wsKisokData[]>(JSONdata); My Class: namespace JSONWebService { [DataContract] [Serializable] public class KisokEvent { public string eventTrigger { get; set; } } [DataContract] [Serializable] public class KisokData { public string data { get; set; } } [DataContract] [Serializable] public class wsKisokData { public KisokEvent KDEvent { get; set; } public List<KisokData> KDData { get; set; } } } I am sure that I don't understand the Deserialize process. Thanks for the help. EDIT: I put the JSON in the top part right from the debugger, here is the strings. { "Event": "sConnection", "data[device]": "fb16f550-2ef1-11e5-afe9-ff37129acbf4", "data[mode]": "customer", "data[starttime]": "2015-07-22T16:07:42.030Z", "data[endtime]": "" } { "Event": "Log", "data[id]": "2015-07-22T16:07:23.063Z", "data[messages][0][source]": "server", "data[messages][0][message]": "Server is listening on port 1553" }
I would HIGHLY recommend using the json.net package off nuget instead. You can generate template classes (models) for it by pasting the json into http://json2csharp.com/ Then use said models to convert the json into a c# object (deserializing) by doing a var jsonStructure = JsonConvert.DeserializeObject<model>(json) And query as if it was just a standard object foreach (var x in jsonStructure.KDData) { doAction(x.data); } // for example
RestSharp: Converting results
I get the following JSON that I am trying to convert to a business object using RestSharp { "valid":true, "data":[ { "dealerId":"4373", "branchId":"4373", } ] } I wish to convert to: public class Dealer { public string dealerId ; public string branchId; } But this fails, though the JSON is fine: var client = new RestClient("http://www.????.com.au"); var request = new RestRequest(string.Format("service/autocomplete/dealer/{0}/{1}.json", suburb.PostCode, suburb.City.Trim().Replace(" ", "%20")), Method.GET); var response2 = client.Execute<Dealer>(request); return response2.Data;
Your business object doesn't match the response JSON you are getting back. If you want your response to serialize, your C# object would look something like public class DealerResponse { public bool valid { get;set; } List<Dealer> data { get;set; } } public class Dealer { public string dealerId; public string branchId; } I haven't tested this code, but even though you are only interested in the information in 'data', your response C# objects still need to represent the whole JSON response to serialize correctly. Hope that helps.