debugcsharpMinor
C# Regex to extract bug ID from Bugzilla JSON response
Viewed 0 times
bugresponseextractjsonregexfrombugzilla
Problem
I have written a series of
I have done some experimentation but because have not figured out how to tell it "Find the { then the } and drop only those but keep the text in between".
Here is my full code block:
The overall goal for this (which is working now). Is to pass data to BugZilla to log exceptions from the app as bugs, get the new bug ID and pass that up to an error page
Regex.Replace() lines to take an input like {"id":36} and convert it to id 36 and I was wondering if it is possible to combine all the calls I am making into one.Results = Regex.Replace(Results, @"\{", "");
Results = Regex.Replace(Results, @"\}", "");
Results = Regex.Replace(Results, @"\""", "");
Results = Regex.Replace(Results, @"\:", " ");I have done some experimentation but because have not figured out how to tell it "Find the { then the } and drop only those but keep the text in between".
Here is my full code block:
[Route("test")]
public async Task Test()
{
Dictionary BugData = new Dictionary
{
{ "Bugzilla_api_key", "Removed For Security" },
{ "product", "Test" },
{ "component", "Test Component" },
{ "version", "unspecified" },
{ "summary", "App API Test" },
{ "op_sys", "All" },
{ "platform", "Other" },
{ "description", "A basic API test" }
};
string Json = JsonConvert.SerializeObject(BugData, Formatting.None);
var Client = new HttpClient();
var Request = new HttpRequestMessage(HttpMethod.Post, "http://bugzilla-tools/rest/bug");
Request.Content = new StringContent(Json, Encoding.UTF8, "application/json");
Request.Headers.Add("Accept", "application/json");
var Response = await Client.SendAsync(Request);
var Results = await Response.Content.ReadAsStringAsync();
Results = Regex.Replace(Results, @"\{", "");
Results = Regex.Replace(Results, @"\}", "");
Results = Regex.Replace(Results, @"\""", "");
Results = Regex.Replace(Results, @"\:", " ");
if (Response.StatusCode == HttpStatusCode.OK)
{
return Results;
}
else
{
return "Error Logging to Bugzilla";
}
}The overall goal for this (which is working now). Is to pass data to BugZilla to log exceptions from the app as bugs, get the new bug ID and pass that up to an error page
Solution
You want to say "any of these characters", so what you want is a character class.
However, if your task is actually to extract a key and value out of JSON, the right tool to use is not a string replacement hack, but a JSON parser.
In this case, the problem with
… is that you're telling it to parse a key-value pair as a list. What you want to do is deserialize a dictionary:
Note that you should check the HTTP status code before trying to process the body of the result.
However, if your task is actually to extract a key and value out of JSON, the right tool to use is not a string replacement hack, but a JSON parser.
In this case, the problem with
JsonConvert.DeserializeObject>(Json)… is that you're telling it to parse a key-value pair as a list. What you want to do is deserialize a dictionary:
JsonConvert.DeserializeObject>(Json)Note that you should check the HTTP status code before trying to process the body of the result.
Code Snippets
JsonConvert.DeserializeObject<List<string>>(Json)JsonConvert.DeserializeObject<Dictionary<string, int>>(Json)Context
StackExchange Code Review Q#118562, answer score: 5
Revisions (0)
No revisions yet.