6
\$\begingroup\$

I have written a series of 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<string> Test() { Dictionary<string, string> BugData = new Dictionary<string, string> { { "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 which will read something like "An error was encountered processing you request. This error has been logged with the bug ID 36" (Where ID 36 would be what this task passes up to the error page).

I am using the Regexes to make the returned string more readable, as trying to deserialize it with NewtonSoft would produce the error:

JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[System.String]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'Bugzilla_api_key', line 1, position 20

From the code:

var T = JsonConvert.DeserializeObject<List<string>>(Json); Results = string.Concat(T[0], " ", T[1]); 
\$\endgroup\$

    1 Answer 1

    5
    \$\begingroup\$

    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

    JsonConvert.DeserializeObject<List<string>>(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<Dictionary<string, int>>(Json) 

    Note that you should check the HTTP status code before trying to process the body of the result.

    \$\endgroup\$
    0

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.