1

I have a requirement in c# to extract the below JSON error message and read the title element. I need to remove all the characters in the string and I want only starting from errors i.e

{   
"errors": 
         [{       
            "status": "404",       
            "title":  "Not found data",
            "detail": "This is a sample line of error detail."     
         }] 
}

Please note that the exception can be anything so I just require to extract the JSON message starting from"errors".

Can you please assist me?

Code

string sb="{465F6CE7-3DF9-4BAF-8DD0-3E116CDAC9E7}0xc0c0167a0System.Net.WebException: There was no endpoint listening at http://TestData/member that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
{   "errors": [     {       "status": "404",       "title":  "Not found data","detail": "This is a sample line of error detail."     }   ] }";
3
  • In the example there are a newline followed by a {.... You could sb.IndexOf("\r\n{") or sb.IndexOf("{") to find the beggining of the json. Commented Mar 10, 2017 at 11:00
  • sb.Substring(sb.IndexOf("{", 1))) - for the case posted above it will work, it obviously depends on what type of messages you'll be working with, the information you provided is not enough for a general purpose solution so you'll need to tweak it. This will skip the first character and look for the first occurence of { and take everything from that point to the end. Commented Mar 10, 2017 at 11:06
  • I'm using a regex after the Errors -I'm able to extract from '[' till ']' var regexp = new Regex("([[].+[]])"); So I believe it's ok for me to extract from this part and I will use the Indexof to select the Title value. Thanks! Commented Mar 10, 2017 at 11:17

3 Answers 3

1

If you're asking how to extract a specific sequence of text from a random string of text, this sounds like a regular expression.

The lazy mans solution: If you're just looking to read the title, you could just do IndexOf on "title", and then read to the next quotation mark that's not preceded by a backward-slash.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm using a regex after the Errors -I'm able to extract from '[' till ']' var regexp = new Regex("([[].+[]])") var matchstring= regex.match(sb); So I believe it's ok for to extract from this part and I will use the Indexof to select the Title value. Thanks!
0
    var pattern = @"\{(\s?)\'errors.*";
    string sb = "{465F6CE7-3DF9-4BAF-8DD0-3E116CDAC9E7}0xc0c0167a0System.Net.WebException: There was no endpoint listening at http://TestData/member that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details. { 'errors': [ { 'status': '404', 'title': 'Not found data','detail': 'This is a sample line of error detail.' } ] }";
    MatchCollection matches = Regex.Matches(sb, pattern);

I have changed the " to ', so just change the pattern to match ".

matches is not an array of all matches matches[0] will give you what you want.

Comments

0

You can use JSON.NET. So, you need to parse your string into JObject i.e.

string sb = @"{   ""errors"": [     {       ""status"": ""404"",       ""title"":  ""Not found data"",""detail"": ""This is a sample line of error detail.""     }   ] }";

JObject jsonObject = JObject.Parse(sb);
JArray errors = (JArray)jsonObject["errors"];

foreach(var item in errors.Children())
{
     int itemStatus = (int)item["status"];
     string itemTitle = (string)item["title"];
     string itemDetail = (string)item["detail"];
}

So, in this loop you can get what you want i have shown all the elements from the JSON that can be extracted.

Hope this helps you :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.