I have a json string which is constructed by the following code:
string path1 = "C:\\Program Files (x86)\\IMAGE\\model\\net.mat";
string path2 = "C:\\Program Files (x86)\\IMAGE\\png\\Lab.png";
string path3 = "D:\\temp\\";
string[] strs={path1 ,path2 ,path3};
string json = JsonConvert.SerializeObject(strs);
Console.WriteLine(json);
List<string> paths = JsonConvert.DeserializeObject<List<string>>(json);
Console.WriteLine(paths.Count);
and there is no error when I serialize it or deserialize it.
The json string is as follows:
"[\"C:\\\\Program Files (x86)\\\\IMAGE\\\\model\\\\net.mat\",\"C:\\\\Program Files (x86)\\\\IMAGE\\\\png\\\\Lab.png\",\"D:\\\\temp\\\\\"]"
Then I pass the string JSON to a *.exe file and deserialize it.
The string the exe received is as follow,which has changed after pass:
string json="[C:\\\\Program Files (x86)\\\\IMAGE\\\\model\\\\net.mat,C:\\\\Program Files
(x86)\\\\IMAGE\\\\png\\\\Lab.png,D:\\\\temp\\]";
Then when I use the follow code to deserialize it,error appear.The code is as follow:
List<string> paths = JsonConvert.DeserializeObject<List<string>>(json);
The error is:
Unhandled JsonReaderException:Unexpected character encountered while parsing value: C. Path '', line 1, position 2.
I wonder why this is the case. Thanks.
You have to put single quotes around the paths.
string json = "[ 'C:\\Program Files (x86)\\IMAGEDL\\model\\net-e-100.mat',C:\\Program Files(x86)\\IMAGEDL\\Labelpng\\Lab.png, D:\\temp\\ ]";
Related
I am building a request containing a list of parameters, this is a list of endpoints that is read from a file. All these containing "/" characters.
First the file is read as:
pointRef = []
with open("myfolder/" + scope, 'r') as f:
for line in f:
pointRef.append(line.strip())
then passing
params = {'endDate': endDate, 'startDate': startDate, 'pointRef': pointRef}
and executing
r = requests.get(url=url_ranged_multiple, headers=headers, params=params)
and this gives error (I tried other requests by hand and they work), but I noticed that the final url request that is composed by "request.get" contains the "%2F" character instead of "/"
I wonder if this is the problem how can I correct it.
Many thanks in advance
I'm trying to encode a QString into a JSON string, so that I can inject it safely via QWebFrame::evaluateJavaScript(QString("o.text = %1;").arg(???)).
For example, in php using the function json_encode
echo json_encode('HELLO "me"');
The output would be
"HELLO \"me\""
This is the internal representation of the string, within the Json object.
In the same way, using Qt, how can I retrieve the internal representation of a string, as it would be encoded as a value, within a Json formatted string?
It's really not that difficult. Start by building up the structure with QJsonObjects
QJsonObject obj;
obj.insert("tag1", QString("Some text"));
Then use QDocument to get a string in Json format
QJsonDocument doc(obj);
QByteArray data = doc.toJson(QJsonDocument::Compact);
QString jsonString(data);
This will produce a string, in the form of: -
{ "tag1" : "Some Text" }
Separate items into a list, splitting on ':'
QStringList items = jsonString.split(':', QString::SkipEmptyParts);
There should be 2 items in the list, the second being the value section of the Json string
"Some Test"}
Remove the final '}'
QString value = items[1].remove('}');
Of-course, you will need to do error checking and be aware that if you have a ':' or '}' in the original string, then you'll need to check for them first.
Original answer doesn't handle : and } inside of string correctly. A similar approach using array which requires only stripping []:
QString encodeJsonStringLiteral(const QString &value)
{
return QString(
QJsonDocument(
QJsonArray() << value
).toJson(QJsonDocument::Compact)
).mid(1).chopped(1);
}
ab"c'd becomes "ab\"c'd"
Or, if you don't need double quotes around the string, replace with .mid(2).chopped(2)
I am trying to upload a CSV file that has special characters using ServletFileUpload of apache common. But the special characters present in the CSV are being stored as junk characters in the database. The special characters I have are Trademark, registered etc. Following is the code snippet.
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
InputStream stream = item.openStream();
if (item.isFormField()) {
System.out.println("Form field " + name + " with value "
+ Streams.asString(stream, "UTF-8") + " detected.");
}
}
I have tried reading it using BufferendReader, used request.setCharacterEncoding("UTF-8"), tried upload.setHeaderEncoding("UTF-8") and also checked with IOUtils.copy() method, but none of them worked.
Please advice how to get rid of this issue and where it needs to be addressed? Is there anything I need to do beyond servlet code?
Thanks
What database are using? What character set is database using? Characters can be malformed in the database rather than in Java code.
I'm am using Asp.net Mvc3!
I wrote following code to search my file into the folder :-
string TrailorsPath =Server.MapPath( "/ePortfolio/"+cid+"/Icons/"+ fileName);
if (System.IO.File.Exists(TrailorsPath))
But it always give incorrect result:
My file path is like that :-
"D:\SVN_HobbyHomes\HobbyHomesWebApp\ePortfolio\1\Icons"
Try to use the "~" at beginning of the path.
string TrailorsPath =Server.MapPath("~/ePortfolio/"+cid+"/Icons/"+ fileName);
there can be multiple problems
put a breakpoint on the line
string TrailorsPath =Server.MapPath( "/ePortfolio/"+cid+"/Icons/"+ fileName);
and check the value of Server.MapPath( "/ePortfolio/"+cid+"/Icons/"+ fileName)
and to begin with use this
Server.MapPath( "~/ePortfolio/"+cid+"/Icons/"+ fileName)
im creating temporary files to be stored in the Temp folder found by:
string path = System.IO.Path.GetTempPath()
method returns C:\windows\Temp\
i append my filename:
string filename = myfile.txt
System.IO.Path.Combine(path,filename)
However, this gives me a
"Message":"The given path\u0027s format is not supported.","StackTrace":" at System.Security.Util.StringExpressionSet.CanonicalizePath(String path, Boolean needFullPath)\r\n at System.Security.Util.StringExpressionSet.CreateListFromExpressions(String[] str, Boolean needFullPath)\r\n at System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, AccessControlActions control, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList)\r\n at System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess access, AccessControlActions...... and so on
I have tried modifying the rights to the TEMP folder, so that NETWORK SERVICES has rights..
Any ideas?
Your path contains an invalid character. Unicode code is 0027 which is a single quote "'" so you need to check where you are adding a single quote to the path.