I am encountering an unusual issue while trying to retrieve elements from JSON in JavaScript. I fetch a JSON string from a URL using the following code:
// Create Request
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(@"www.someurl.com");
// Create Client
WebClient client = new WebClient();
// Assign Credentials
client.Credentials = new NetworkCredential("username", "pass");
// Grab Data
sjson = client.DownloadString(@"www.someurl.com");
System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
oSerializer.MaxJsonLength = Int32.MaxValue;
sjson = oSerializer.Serialize(sjson);
However, when I try to access the 'sjson' variable from JavaScript in my HTML code, it returns nothing. Interestingly, hardcoding the value works fine. Here is the JavaScript code snippet:
<script type="text/javascript">
var jsons = JSON.parse('<%=sjson%>');
function initialize() {
alert("hahahahaaa");
document.writeln(jsons.Body[0].RowId.SensorIdValue);
//document.writeln(myobject.Body[0].RowId.SensorIdValue);
}
</script>
The problem lies in this line of code:
document.writeln(myobject.Body[0].RowId.SensorIdValue);
It returns a value if I use the 'myobject' variable but returns nothing with the parsed 'jsons'. :(
Here is a sample output of the JSON response that I receive after running the serializer via C#.
If anyone can assist me with this issue, I would greatly appreciate it.
EDIT:
For further context, here is the raw JSON string obtained directly from the server without any serialization processes applied. Some contents have been omitted at the owner's request.