-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyapicontroller.js
99 lines (93 loc) · 3.18 KB
/
myapicontroller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;
using System.Web.Script.Serialization;
using Newtonsoft.Json.Linq;
namespace BeyondLogical.Controllers
{
[EnableCors(origins: "*", headers: "*", methods: "*")]
public class SonyController : ApiController
{
public HttpResponseMessage Get()
{
string myJSON = "";
var json = System.Web.HttpContext.Current.Server.MapPath("~/myfile.json");
using (FileStream mem = new FileStream(json, FileMode.Open))
{
using(StreamReader sr = new StreamReader(mem))
{
myJSON = sr.ReadToEnd();
sr.Close();
sr.Dispose();
}
mem.Close();
mem.Dispose();
}
string myDate = DateTime.Now.ToShortDateString();
return new HttpResponseMessage()
{
Content = new StringContent("GET: " + myJSON)
};
}
public HttpResponseMessage Post([FromBody]Booth jsonBody)
{
var getArrayOfObjects = jsonBody.mystrings;
var serializer = new JavaScriptSerializer();
List<Booth> booths = new List<Booth>();
foreach (var array in getArrayOfObjects)
{
var serializearray = JArray.Parse(array);
foreach (var myobject in serializearray)
{
var id = "";
var name = "";
var detail = "";
var tryparse = JObject.Parse(myobject.ToString());
foreach (var attrs in tryparse)
{
var mykey = attrs.Key;
if(mykey == "id") {
id = attrs.Value.ToString();
}
else if (mykey == "name")
{
name = attrs.Value.ToString();
}
else if (mykey == "detail")
{
detail = attrs.Value.ToString();
}
}
booths.Add(new Booth
{
id = id,
name= name,
detail = detail
});
}
}
var stophere = "";
var serializedFinal = serializer.Serialize(booths);
string updatedFileContents = serializedFinal;
System.IO.File.WriteAllText(System.Web.HttpContext.Current.Server.MapPath("~/myfile.json"), updatedFileContents);
return new HttpResponseMessage()
{
Content = new StringContent(serializedFinal)
};
}
}
}
public class Booths
{
public List<Booth> booths { get; set; }
}
public class Booth
{
public string id { get; set; }
public string name { get; set; }
public string detail { get; set; }
public List<string> mystrings { get; set; }
}