2

I'm stuck on a issue where i get a string from a web request containing html, but inside that html is a json object that i need to parse over to a object to use in my code but i'm stuck on how to do this.

I tried to use IndexOf() and LastIndexOf() but when i try to point them to the first and last curly braces i get a index of -1 and a exception.

Any ideas?

EDIT: I have also tried to convert it to a list of characters and illitterate over it but when it got converted the curly braces were gone and the position was a empty entry.

EDIT2:

Added the html i get from the request, its lines 3-5 that i need to extract.

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<body onload="parent.postMessage('redirectResponse=
{"messageId":"4232450191","errorCode":0,"sessionToken":
{"sessionToken":"tRabFfRPwYX4fGdHZOrBYDAAoICwwCDo","issuerSystemId":"380","creationTime":
{"timestamp":"2016-02-11T08:58:30.000+00:00"},"expirationTime":
{"timestamp":"2016-02-11T09:03:30.000+00:00"},"maxIdlePeriod":0},
"realMode":1,"username":"myUserName"}
', 'https://target.site.com');"></body></html>
Threezool
  • 453
  • 4
  • 11
  • How are we going to help you, if you don't help us to understand what you are doing? What libraries are you using? What does the relevant part of your code look like? – Allmighty Feb 11 '16 at 10:12
  • I'm using c# .net and im trying to extract the json object from the web request i got from the server. I'm working on a log in function to that site with my program. – Threezool Feb 11 '16 at 10:47

3 Answers3

3
  1. You can use a Regex to cut the Json text.
  2. Parse the Json text using Newtonsoft.Json package.
string htmlText = Resources.html;
string jsonPtn = @"\{(?:[^\{\}]|(?<o>\{)|(?<-o>\}))+(?(o)(?!))\}";
string input = htmlText.Substring(htmlText.IndexOf("redirectResponse="));
Match match = Regex.Matches(input, jsonPtn, RegexOptions.Multiline | RegexOptions.IgnoreCase)[0];
string jsonText = match.Groups[0].Value;
var jsonObj = JObject.Parse(jsonText);

The jsonObj will be like:

{{ "messageId": "4232450191", "errorCode": 0, "sessionToken": { "sessionToken": "tRabFfRPwYX4fGdHZOrBYDAAoICwwCDo", "issuerSystemId": "380", "creationTime": { "timestamp": "2016-02-11T03:58:30-05:00" }, "expirationTime": { "timestamp": "2016-02-11T04:03:30-05:00" }, "maxIdlePeriod": 0 }, "realMode": 1, "username": "myUserName" }}

Shangwu
  • 1,440
  • 12
  • 12
0

Could you please provide the html string you received??

Update:

Could be the problem with the encoding....

Try:

Encoding trouble with HttpWebResponse

or

Is it possible to get data from web response in a right encoding

                if (response.CharacterSet == null)
                {
                    readStream = new StreamReader(receiveStream);
                }
                else
                {
                    readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
                }

If you couldn't find the solution in the above links, then post the code u are using...

Community
  • 1
  • 1
0

public class MyHtmlTagRemover {

public static void main(String a[]){
    String text = "<B>I don't want this to be bold<\\B>";
    System.out.println(text);
    text = text.replaceAll("\\<.*?\\>", "");
    System.out.println(text);
}

}

swati
  • 1