Json result format in mvc - asp.net-mvc

I want to read the text file into json. i have a text file with three lines.
While i tried to read using json .It returns all the 3 lines into one string.
Here is my code for your reference :
[System.Web.Mvc.HttpPost]
public JsonResult upload(HttpPostedFileBase file)
{
bool result = false;
StringBuilder strbuild = new StringBuilder();
try
{
if (file.ContentLength == 0)
throw new Exception("Zero length file!");
else
{
var fileName = Path.GetFileName(file.FileName);
var filePath = Path.Combine(Server.MapPath("~/Downloads"), fileName);
file.SaveAs(filePath);
if (!string.IsNullOrEmpty(filePath))
{
using (StreamReader sr = new StreamReader(Path.Combine(Server.MapPath("~/Downloads"), fileName)))
{
while (sr.Peek() >= 0)
{
strbuild.AppendFormat(sr.ReadLine());
}
}
}
}
}
catch (Exception)
{
result = false;
}
return new JsonResult()
{
Data = strbuild.ToString()
};
}
My Result is :
"HD-M-16-H-000001*1001*1 HD-M-16-H-000001*1001 HD-M-16-H-000001 15 JUL 201614 JUL 20170816ACHEONG SIEW FUNG 12345678I 22 DEC 1960SPO F- 15 JUL 201615 JUL 2016 HD-M-16-H-000001*1001*2 HD-M-16-H-000001*1001 HD-M-16-H-000001 15 JUL 201614 JUL 20170816AREBECCA SIOW SIQIN S1234567E 04 FEB 1995C O F- 15 JUL 201615 JUL 2016 HD-M-16-H-000001*1001*3 HD-M-16-H-000001*1001 HD-M-16-H-000001 15 JUL 201614 JUL 20170816ASAMANTHA SIOW SIHUI S1234567J 18 NOV 1993C O F- 15 JUL 201615 JUL 2016 "
My Text File is :
line 1:
HD-M-16-H-000001*1001*1 HD-M-16-H-000001*1001 HD-M-16-H-000001 15 JUL 201614 JUL 20170816ACHEONG SIEW FUNG 12345678I 22 DEC 1960SPO F- 15 JUL 201615 JUL 2016
line 2 :
HD-M-16-H-000001*1001*2 HD-M-16-H-000001*1001 HD-M-16-H-000001 15 JUL 201614 JUL 20170816AREBECCA SIOW SIQIN S1234567E 04 FEB 1995C O F- 15 JUL 201615 JUL 2016
Line 3:
HD-M-16-H-000001*1001*3 HD-M-16-H-000001*1001 HD-M-16-H-000001 15 JUL 201614 JUL 20170816ASAMANTHA SIOW SIHUI S1234567J 18 NOV 1993C O F- 15 JUL 201615 JUL 2016
So how can i show the json result with 3 (String) lines ?
Thanks in advance !!!

Related

DataTables in mvc

I have a string, named s respectively. The string s looks like :
"HD-M-16-H-000001*1001*1 HD-M-16-H-000001*1001 HD-M-16-H 000001 15 JUL 201614 JUL 20170816ACHEONG SIEW FUNG 12345678I 22 DEC 1960SPO F-
HD-M-16-H-000001*1001*2 HD-M-16-H-000001*1001 HD-M-16-H 000001 15 JUL 201614 JUL 20170816ACHEONG SIEW FUNG 12345678I 22 DEC 1960SPO F-
HD-M-16-H-000001*1001*3 HD-M-16-H-000001*1001 HD-M-16-H 000001 15 JUL 201614 JUL 20170816ACHEONG SIEW FUNG 12345678I 22 DEC 1960SPO F- "
I split the string based on rows. The code is
string s = sr.ReadToEnd();
string[] Mem = s.Split('\r');
So, Now the string[] Mem looks like :
"HD-M-16-H-000001*1001*1 HD-M-16-H-000001*1001 HD-M-16-H 000001 15 JUL 201614 JUL 20170816ACHEONG SIEW FUNG 12345678I 22 DEC 1960SPO F- "
"HD-M-16-H-000001*1001*2 HD-M-16-H-000001*1001 HD-M-16-H 000001 15 JUL 201614 JUL 20170816ACHEONG SIEW FUNG 12345678I 22 DEC 1960SPO F- "
"HD-M-16-H-000001*1001*3 HD-M-16-H-000001*1001 HD-M-16-H 000001 15 JUL 201614 JUL 20170816ACHEONG SIEW FUNG 12345678I 22 DEC 1960SPO F- "
Now, actually i am looking to create a dataTable with 'n' number of columns to hold Each word of each string in Mem.
The below code that takes each line of string[] and holds it into string. Then i split the string
string firststr = Mem.First();
string secondstr = Mem[1];
string thirdstr = Mem[2];
string str = firststr.Substring(0, 30);
string[] m = str.Split('\r');
Actually i want to store all the three string[] to one string datatype with 3 strings and split each line of string by substr using loop(foreach or While). So how can i acheive this???
Any help appreciated. Thanks in advance !!!!

Sum the next value of hash in an array of hashes

I have an array of hashes like this:
[{Mon, 09 May 2016 14:49:17 UTC +00:00=>12},
{Sun, 17 Apr 2016 14:08:40 UTC +00:00=>30},
{Sun, 16 Apr 2016 14:08:40 UTC +00:00=>18},
{Sun, 15 Apr 2016 14:03:33 UTC +00:00=>21}]
How can I sum the previous value from the oldest date to the current date, my expected output will be:
[{Mon, 09 May 2016 14:49:17 UTC +00:00=>81},
{Sun, 17 Apr 2016 14:08:40 UTC +00:00=>69},
{Sun, 16 Apr 2016 14:08:40 UTC +00:00=>39},
{Sun, 15 Apr 2016 14:03:33 UTC +00:00=>21}]
Thanks!
Assuming that the key of every hash in your array is a DateTime object, you can get what your want with this:
balance = [
{DateTime.parse('Mon, 09 May 2016 14:49:17 UTC +00:00')=>12},
{DateTime.parse('Sun, 17 Apr 2016 14:08:40 UTC +00:00')=>30},
{DateTime.parse('Sun, 16 Apr 2016 14:08:40 UTC +00:00')=>18},
{DateTime.parse('Sun, 15 Apr 2016 14:03:33 UTC +00:00')=>21}
] # => your original array
# Get expected array.
balance.map{ |h|
{
h.keys.first => balance.select{ |e|
e.keys.first <= h.keys.first }.map{ |s|
s[s.keys.first] }.reduce(:+)
}
}
I split the code in lines in order to improve readability.
Another approach would be to sort the array first and then use the map function to keep a running total to collect the required data.
# sort the balances by date
balance = balance.sort {|a, b| a.keys.first <=> b.keys.first }
# get running total and collect for each date
total = 0
balance.map do |entry|
date, value = entry.first
total += value
{date => total}
end
I assume your array is in lastest-to-earliest date order and looks something like arr below:
a = [{ "Mon, 09 May 2016 14:49:17 UTC +00:00"=>12 },
{ "Sun, 17 Apr 2016 14:08:40 UTC +00:00"=>30 },
{ "Sun, 16 Apr 2016 14:08:40 UTC +00:00"=>18 },
{ "Sun, 15 Apr 2016 14:03:33 UTC +00:00"=>21 }]
require 'date'
arr = a.map do |h|
(d, v) = h.to_a.first
{ DateTime.parse(d) => v }
end
#=> [{#<DateTime: 2016-05-09T14:49:17+00:00 ((2457518j,53357s,0n),+0s,2299161j)>=>12},
# {#<DateTime: 2016-04-17T14:08:40+00:00 ((2457496j,50920s,0n),+0s,2299161j)>=>30},
# {#<DateTime: 2016-04-16T14:08:40+00:00 ((2457495j,50920s,0n),+0s,2299161j)>=>18},
# {#<DateTime: 2016-04-15T14:03:33+00:00 ((2457494j,50613s,0n),+0s,2299161j)>=>21}]
We can then compute the required array as follows.
cumv = 0
arr.reverse.
map { |h| h.to_a.first }.
each_with_object([]) do |(d,v),a|
cumv += v
a << { d => cumv }
end.
reverse
#=> [{#<DateTime: 2016-05-09T14:49:17+00:00 ((2457518j,53357s,0n),+0s,2299161j)>=>81},
# {#<DateTime: 2016-04-17T14:08:40+00:00 ((2457496j,50920s,0n),+0s,2299161j)>=>69},
# {#<DateTime: 2016-04-16T14:08:40+00:00 ((2457495j,50920s,0n),+0s,2299161j)>=>39},
# {#<DateTime: 2016-04-15T14:03:33+00:00 ((2457494j,50613s,0n),+0s,2299161j)>=>21}]

Retrieve content between 2 words

I have a build log which I want to extract some content from. I want to extract [xxxxcontentxxxx] out of the log file given the system name, if any.
Input.txt:
150755: CMDLINE=omake System1
150758: Subsystem Start: Tue Dec 22 03:33:06 2015 1450726386
[xxxxcontentxxxx]
151251: Subsystem End: Tue Dec 22 03:35:00 2015 1450726500
151255: CMDLINE=omake System2
151258: Subsystem Start: Tue Dec 22 03:35:00 2015 1450726500
151668: Subsystem End: Tue Dec 22 03:36:28 2015 1450726588
151672: CMDLINE=omake System3
151675: Subsystem Start: Tue Dec 22 03:36:29 2015 1450726589
[xxxxcontentxxxx]
152020: Subsystem End: Tue Dec 22 03:38:14 2015 1450726694
152024: CMDLINE=omake System4
152027: Subsystem Start: Tue Dec 22 03:38:15 2015 1450726695
[xxxxcontentxxxx]
152294: Subsystem End: Tue Dec 22 03:39:12 2015 1450726752
152298: CMDLINE=omake System5
152301: Subsystem Start: Tue Dec 22 03:39:12 2015 1450726752
[xxxxcontentxxxx]
152558: Subsystem End: Tue Dec 22 03:40:18 2015 1450726818
For example, I want to extract for System2, System3 and System4. The output should be saved to a file in the following format:
Note: System2 is skipped because it does not contain [xxxxcontentxxxx]
Output.txt:
151672: CMDLINE=omake System3
151675: Subsystem Start: Tue Dec 22 03:36:29 2015 1450726589
[xxxxcontentxxxx]
152020: Subsystem End: Tue Dec 22 03:38:14 2015 1450726694
152024: CMDLINE=omake System4
152027: Subsystem Start: Tue Dec 22 03:38:15 2015 1450726695
[xxxxcontentxxxx]
152294: Subsystem End: Tue Dec 22 03:39:12 2015 1450726752
How can I achieve this with PowerShell?
Here is something I have. Certainly, there is a more elegant way of achieving this:
$reader = [System.IO.File]::OpenText("C:\log.txt")
try {
$startPrinting = $false
for (;;) {
$line = $reader.ReadLine()
if ($line -eq $null) { break }
if ($line.Split(' ').Contains("System2") -or
$line.Split(' ').Contains("System3") -or
$line.Split(' ').Contains("System4")
) {
$startPrinting = $true
}
if ($startPrinting) {
Write-Host $line
}
if ($line.split(' ').Contains('End:')) {
$startPrinting = $false
}
}
} finally {
$reader.Close()
}
You could for instance use a switch statement, like this:
$systems = 'System2', 'System3', 'System4'
$reader = [IO.File]::OpenText('.\Documents\test.txt')
while ($line = $reader.ReadLine()) {
switch -regex ($line) {
'CMDLINE=omake (\S+)' {
$systemName = $matches[1]
$msg = #($line)
}
'Subsystem End' {
if ($systems -contains $systemName) {
if ($msg.Count -gt 3) {
$msg -join "`r`n"
} else {
"Note: $systemName is skipped because it doesn't have content."
}
}
}
default { $msg += $line }
}
}
$reader.Close()
This is, of course, entirely unpolished as it assumes that your input file will always start with a CMDLINE line and end with a Subsystem End line. But it should give you the general idea.

Is my gradient checking method correct and my gradient calculation wrong, or vice versa?

My network only achieve around 80%, but the reported best score is around 85% accuracy. I m using same input data and same initalization. I dont know whats wrong, so I try to check my gradients and implemented what is recommended for gradient checking: http://ufldl.stanford.edu/tutorial/supervised/DebuggingGradientChecking/
But i m not sure, if my implementation is correct:
public void gradientchecking(double[] theta){
System.out.println("Gradient Checking started");
//costfunction returns cost and gradients
IPair<Double, double[]> org = costfunction(theta);
double[] theta_pos = new double[theta.length];
double[] theta_neg = new double[theta.length];
for (int i = 0; i < theta.length; i++) {
theta_pos[i]= theta[i];
theta_neg[i]=theta[i];
}
double mu = 1e-5;
for (int k = 0; k < 20; k++) {
theta_pos[k] = theta_pos[k] + mu;
theta_neg[k] = theta_neg[k] - mu;
IPair<Double, double[]> pos = costfunction(theta_pos);
IPair<Double, double[]> neg = costfunction(theta_neg);
System.out.println("Org: "+org.getSecond()[k] +" check:"+ ((pos.getSecond()[k]-neg.getSecond()[k])/(2*mu)));
//System.out.println("Org: "+org.getSecond()[k] +"check:"+ ((pos.getSecond()[k]-neg.getSecond()[k])/(2*mu)));
theta_pos[k] = theta_pos[k] - mu;
theta_neg[k] = theta_neg[k] + mu;
}
}
}
I got the following result after a freshly initialized theta:
Gradient Checking started
Cost: 1.1287071297725055 | Wrong: 124 | start: Thu Jul 30 22:57:08 CEST 2015 |end: Thu Jul 30 22:57:18 CEST 2015
Cost: 1.128707130295382 | Wrong: 124 | start: Thu Jul 30 22:57:18 CEST 2015 |end: Thu Jul 30 22:57:28 CEST 2015
Cost: 1.1287071292496391 | Wrong: 124 | start: Thu Jul 30 22:57:28 CEST 2015 |end: Thu Jul 30 22:57:38 CEST 2015
Org: 5.2287135944026004E-5 check:1.0184607936733826E-4
Cost: 1.1287071299252593 | Wrong: 124 | start: Thu Jul 30 22:57:38 CEST 2015 |end: Thu Jul 30 22:57:47 CEST 2015
Cost: 1.1287071296197628 | Wrong: 124 | start: Thu Jul 30 22:57:47 CEST 2015 |end: Thu Jul 30 22:57:56 CEST 2015
Org: 1.5274823511207024E-5 check:1.141254586229615E-4
Cost: 1.1287071299063134 | Wrong: 124 | start: Thu Jul 30 22:57:56 CEST 2015 |end: Thu Jul 30 22:58:05 CEST 2015
Cost: 1.1287071296387077 | Wrong: 124 | start: Thu Jul 30 22:58:05 CEST 2015 |end: Thu Jul 30 22:58:14 CEST 2015
Org: 1.3380293717695182E-5 check:1.0008639478696018E-4
Cost: 1.1287071297943114 | Wrong: 124 | start: Thu Jul 30 22:58:14 CEST 2015 |end: Thu Jul 30 22:58:23 CEST 2015
Cost: 1.1287071297507094 | Wrong: 124 | start: Thu Jul 30 22:58:23 CEST 2015 |end: Thu Jul 30 22:58:32 CEST 2015
Org: 2.1800899147740388E-6 check:9.980780136716263E-5
that indicates that my gradient calculation has an error, or the gradientchecking() method. I m not sure, can somebody help me?
In Java arrays are reference types.
int[] arr = { 8,7,6,5,4,3,2,1,8};
int[] b = arr;
b [0] = -10;
for (int i:arr) {
System.out.print (' ');
System.out.print (i);
}
outputs -10 7 6 5 4 3 2 1 8
So i mean that you incorrectly creating arrays
double[] theta_pos = theta;
double[] theta_neg = theta;
they are just references to theta, and by changing their contents you change theta also, +mu-mu = 0. Use clone() methods while copying array.
double[] theta_pos = theta.clone();
double[] theta_neg = theta.clone();
But remember that clone may not work as you expecting in some cases, with simple(non-reference) types it works ideal. Look at this
Does calling clone() on an array also clone its contents?

MFC , Any solution to parse ftp response file list line

Have any library solution to parse as follow file list string
0x001d8f60 "-rw------- 1 root root 000000000 Jun 30 2014 YS_LIANG
drw------- 1 root root 000000000 Jun 30 2014 2014-05-08 [][][] - [][][][]
drw------- 1 root root 000000000 Jun 30 2014 FTPClient-WinCE
drw------- 1 root root 000000000 Jun 30 2014 GRAPHIC
drw------- 1 root root 000000000 Jun 30 2014 S8521 V1.6.17
drw------- 1 root root 000000000 Jun 30 2014 S8521V1.7.0
drw------- 1 root root 000000000 Jun 30 2014 Storage Card2
drw------- 1 root root 000000000 Jun 30 2014 UltraEdit 18.20.0.1021
drw------- 1 root root 000000000 Jun 30 2014 usb wireless driver
drw------- 1 root root 000000000 Jun 30 2014 Windows7 ActiveSync
-rw------- 1 root root 101384240 Jun 28 2013 avira_free_antivirus_zhtw.exe
-rw------- 1 root root 000564736 Jun 21 2011 ComTest.exe
-rw------- 1 root root 000000189 Feb 10 2014 ComTest.INI
-rw------- 1 root root 006465536 May 5 2014 DR12AsyncSocket.exe
-r"
Thanks
ok, I got the solution.
while(true)
{
nNewLineIdx = strRespFileList.Find(_T("\r\n"));
if(nNewLineIdx == -1)
{
break;
}
// TODO : Save in CStringArray
}
// Parse CString line of CStringArray
for(int loop=0; loop<CStringArray.GetCount();loop++)
{
int strLineLen = CStringArray.GetAt(loop).GetLength();
int nSerchLoc = 0;
int nStep = 0;
while(nSerchLoc < strLineLen)
{
CString strTok = CStringArray.GetAt(loop).Tokenize(_T(" "), nSerchLoc);
// TODO : Save cut information
}
}

Resources