How to remove the first and last character in JObject in c# - trim

string jsonstring = JsonConvert.SerializeObject(dict, Formatting.Indented);
JObject json = JObject.Parse(jsonstring );
I am using this code first and last character needs to be removed...whenever i am converting json string to jobject curly braces gets included in first and last character of the object ...How should i remove this or is there any other way to convert json string to jobject

Related

How to avoid Java request.getQueryString() get escaped string [duplicate]

This question already has answers here:
request.getQueryString() seems to need some encoding
(5 answers)
Closed 2 years ago.
I have a Servlet that process a QueryString from a JSP form.
When the JSP send the information to the Servlet, it puts on the URL the query, like this:
https://www.mywebsite.com/search?query=Barcelona
And i receive the data inside the Servlet in this way:
String query = request.getQueryString();
// query will receive "Barcelona"
Everything works alright. The problem comes when the string contains characters from other encodings like russian, chinese, arabic, etc. So, if I put the URL like:
https://www.mywebsite.com/search?query=Медеуский
I will receive in the query variable the following text:
String query = request.getQueryString();
// query will receive " %D0%9C%D0%B5%D0%B4%D0%B5%D1%83%D1%81%D0%BA%D0%B8%D0%B9"
So, how could I avoid the escaped conversion?
Thanks!
You can use URLDecoder.decode(request.getQueryString(), "UTF-8")
or
String unicodeQuery = new String(query.getBytes(), StandardCharsets.UTF_8);
String unicodeQuery = new String(query.getBytes("ISO_8859_1"), StandardCharsets.UTF_8);
String unicodeQuery = new String(query.getBytes(Charset.defaultCharset()), StandardCharsets.UTF_8);

Spring MVC, #RequestParam initialize default list with one empty string

Is there a way to initialize a default list with one empty string using the #RequestParam annotation in Spring MVC?
If I use the following code, the list is empty:
public String example(
#RequestParam(value = "example", defaultValue = "")
List<String> exampleList) {
return "";
}
Using defaultValue = "," instead, results in a list with two empty strings. My current solution is to check if the list is empty and if so add the empty string to it but is there a better way?
Instead of defaultValue = "," use defaultValue = " ". It's work and your list is initialise with one empty string.

Newtonsoft Json JsonSerializationException with simple string

My datasource creates the JSON representing an array of integers as "1,2,3,4,5". I can't do anything about this (Like changing it to [1,2,3,4,5]), it is an enterprise CMS that we have to just deal with.
I'm trying to read up on how the newtonsoft ToObject method handles the following code:
JValue theValue = new JValue("1,2,3")
List<int> x = theValue.ToObject<List<int>>();
I get a Newtonsoft.Json.JsonSerializationException. Could not cast or convert from System.String to System.Collections.Generic.List`1[System.String]. I understand this fully, but I'd like to know if the Newtonsoft JSON libraries have a built in way to convert from a comma delimited string to a List.
I'd like to think there's a better way than trying to check if the variable is a comma delimited list or not and then converting it to a List<> manually, or maybe a JArray, but I've been wrong before !
EDIT
I wanted to share my solution:
dynamic theValue = new JValue("1,2,3,4"); /// This is just passed in, i'm not doing this on purpose. Its to demo.
if (info.PropertyType == typeof (List<int>))
{
if (info.CanWrite)
{
if (theValue.GetType() == typeof (JValue) && theValue.Value is string)
{
theValue = JArray.Parse("[" + theValue.Value + "]");
}
info.SetValue(this, theValue.ToObject<List<int>>());
}
} else {
// do other things
You have three problems from what I can see:
You should be using JArray not JValue. You are intending this to be an array of things, so you need to use the equivalent class in Newtonsoft to represent an array. (A JValue, as best I can tell, represents a simple type--e.g. string, number, Date, etc.)
You should use the Parse method versus using the constructor. Parse will read the content of the string as an array, however...
...in order for it to do that, you will need to surround the data that you get with the square brackets or JArray can't correctly the parse the data. There is no need to fiddle with the CMS; just do a string concat before you parse.
e.g.
JArray theValue = JArray.Parse("[" + "1,2,3" + "]");

Looks like a query string, but won't act as a query string

I am working with VB in asp.net,
The basic problem is, I want to pair up the elements in a string, exactly like request.QueryString() will do to the elements in the query string of the web page.
However instead of the function looking at the current webpage query string I want it to look at a string (that is in the exact form of a query string) stored as a variable.
So if I define a string such as:
Dim LooksLikeAQueryString As String = "?category1=answer1&category2=answer2"
I want a function that if I input LooksLikeAQueryString and "category1" it outputs "answer1" etc.
Is there anything that can already do this or do I have to build my own function? If I have to build my own, any tips?
I should add that in this case I won't be able to append the string to the url and then run request.QueryString.
You can use the HttpUtility.ParseQueryString method - MSDN link
ParseQueryString will do it for you - something along these lines:
Private Function QueryStringValue(queryString As String, key As String) As String
Dim qscoll As NameValueCollection = HttpUtility.ParseQueryString(queryString)
For Each s As String In qscoll.AllKeys
If s = key Then Return qscoll(s)
Next
Return Nothing
End Function
Usage:
Dim LooksLikeAQueryString As String = "?category1=answer1&category2=answer2"
Response.Write(QueryStringValue(LooksLikeAQueryString, "category2"))
If you dont want the dependancy of System.Web, of the top of my head
public string GetValue(string fakeQueryString,string key)
{
return fakeQueryString.Replace("?",String.Empty).Split('&')
.FirstOrDefault(item=>item.Split('=')[0] == key);
}

How to pass a Date object from Flex to a JSP Servlet?

I am passing some parameters as POST to a Servlet from my Flex WebApplication. I am able to pass and retrieve strings properly.
I used new Date().getTime() in my flex code and tried passing the timestamp variable hoping to parse it at the servlet and convertit into a java.util.Date object. But i am getting a NumberFormatException when i try to parse the variable from the string that i got from request.getParameter.
Any solutions?
I think you are not converting String to long in Java IF so Please try
String strDateinmilliseconds = request.getAttribute("FlexMilliseconds");
long dateinmilliseconds = Long.valueOf(strDateinmilliseconds);
Date resultdate = new Date(dateinmilliseconds);
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
System.out.println(sdf.format(resultdate));
Hopes that helps

Resources