3

I have the following GET request:

string url = @"http://api.flexianalysis.com/services/flexianalysisservice.svc/TechnicalAnalysisByCategory?clientid=___&category=forex&key=____";

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.AutomaticDecompression = DecompressionMethods.GZip;

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        using (Stream stream = response.GetResponseStream())
        using (StreamReader reader = new StreamReader(stream))
        {
             rawJson = new StreamReader(response.GetResponseStream()).ReadToEnd();
            //html = reader.ReadToEnd();
        }

This is an example of the JSON:

[{"ID":133739,"TickerID":23,"CategoryID":3,"ClientID":5044,"TickerDateTime":"2017-11-06T12:57:19.267","TickerTitle":"AUD/USD Intraday: key resistance at 0.7670.\r\n","Category":"Forex","TradePairName":"AUD/USD","Ticker":"AUD","TrendType":"THE upside prevails","Status":"Enabled","TrendValue1":"+1","PivotValue":0.767,"OurPreference":"v: short positions below 0.7670 with targets at 0.7635 & 0.7615 in extension.\r\n","AlternateScenario":"o: above 0.7670 look for further upside with 0.7695 & 0.7715 as targets.\r\n","Comments":" as long as 0.7670 is resistance, look for choppy price action with a bearish bias.\r\n","S1":0.7635,"S2":0.7615,"S3":0.7595,"R1":0.767,"R2":0.7695,"R3":0.7715,"Entry":0.0,"Stop":0.0,"T1":0.0,"T2":0.0},{"ID":133738,"TickerID":193,"CategoryID":3,"ClientID":5044,"TickerDateTime":"2017-11-06T12:55:54.33","TickerTitle":"Dollar Index‏ (ICE) Intraday: bullish bias above 94.8000.\r\n","Category":"Forex","TradePairName":"Dollar Index (ICE)","Ticker":"DXA","TrendType":"THE upside prevails","Status":"Enabled","TrendValue1":"+1""PivotValue":94.8,"OurPreference":": long positions above 94.8000 with targets at 95.1500 & 95.3000 in extension.\r\n","AlternateScenario":"below 94.8000 look for further downside with 94.6500 & 94.4500 as targets.\r\n","Comments":": the RSI lacks downward momentum.","S1":94.8,"S2":94.65,"S3":94.45,"R1":95.15,"R2":95.3,"R3":95.45,"Entry":0.0,"Stop":0.0,"T1":0.0,"T2":0.0}]

Then i am trying to parse it to JSON and remove the 'd' at the beginning:

var json = JObject.Parse(rawJson);
        var filter = json["d"];
        var fff = filter["ID"];//Get the error here

Now I want to read the ID but for some reason it gives an error that it can't access child node. Any idea why?

Zhaph - Ben Duguid
  • 26,785
  • 5
  • 80
  • 117
Igal C
  • 45
  • 1
  • 9
  • Please explain in what line do you get the error and the exact error message. – Steve Nov 06 '17 at 13:05
  • 2
    Is the `key` and `clientId` request private for your usage? If so, please flag the mods to remove it from edit history – Icepickle Nov 06 '17 at 13:05
  • 1
    @Icepickle Already flagged ;) – Zhaph - Ben Duguid Nov 06 '17 at 13:06
  • 5
    Did you just post your client id and access key? You need to invalidate these immediately and request a new access key. Everybody can see edits and old history. Even if moderators edit out the history here, enough people have probably already seen it to make it security issue. You do not want to be that guy that leaked confidential data to strangers on the internet. – Lasse V. Karlsen Nov 06 '17 at 13:06
  • 3
    The actual question cannot be answered because nobody here knows what it means to "remove the 'd' at the beginning". You need to post an example of the json you want to handle, and explain more what kind of handling you want. As for the error message, you need to post that error message along with the actual code that produces it. – Lasse V. Karlsen Nov 06 '17 at 13:09
  • If you get an error on that line, the JSON does not match what you are expecting, and we cannot help without knowing what the JSON data looks like. – crashmstr Nov 06 '17 at 13:31
  • I added the relevant data – Igal C Nov 06 '17 at 13:56
  • @LasseVågsætherKarlsen - A JSON response from an ASP.NET server is often returned from the server as the child of an object `{d: {[actualObjectHere]}}` This was introduced back in .NET 3.5 to protect against some XSS attacks. – Zhaph - Ben Duguid Nov 06 '17 at 14:01
  • @IgalC Please stop including your key in the question - this is sensitive, private information that you should not be posting on a public website. – Zhaph - Ben Duguid Nov 06 '17 at 14:02
  • Possible duplicate of [Json Deserialization parsing non valid Json object](https://stackoverflow.com/questions/42649558/json-deserialization-parsing-non-valid-json-object) – Zhaph - Ben Duguid Nov 06 '17 at 14:35

1 Answers1

3

I think you need to check a few of your assumptions and try some break points.

Looking at the JSON returned by that API it looks like it's poorly formed and you are actually receiving a JSON array as a string:

{"d":"[{\"ID\":133739,\"TickerID\":23,\"CategoryID\":3,...}},
       {\"ID\":133740,\"TickerID\":23,\"CategoryID\":3,...}},
       [...]}]"}

Thus to parse it you'll first need to grab the value from the d parameter, and then parse that into an array:

// Get the response from the server
using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
{
    // Pass the response into a stream reader
    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
    {
        // Grab the JSON response as a string
        string rawJson = reader.ReadToEnd();

        // Parse the string into a JObject
        var json = JObject.Parse(rawJson);

        // Get the JToken representing the ASP.NET "d" parameter
        var d = json.GetValue("d");

        // Parse the string value of the object into a jArray
        var jArray = JArray.Parse(d.ToString());

        // At this point you can start looking for the items.
    }
}
Zhaph - Ben Duguid
  • 26,785
  • 5
  • 80
  • 117