Reading from JSON, data not displayed

2019-09-02 02:00发布

问题:

I'm having this strange problem when i try to access elements in json from javascript. i retreve a json string from a url like so,

        // 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);

But when i access this sjson variable from javascript in html code, it doesn't return anything, but if i hard code it, it returns the values, please help on this one. I tried many things but didn't work. I also tried to just pass the retreieved json string without serializing, when i do that the javascript stops working. :( following is the javascript code,

    <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 issue is

    document.writeln(myobject.Body[0].RowId.SensorIdValue); 

returns a value if i use the myobject variable, but

    document.writeln(jsons.Body[0].RowId.SensorIdValue);

returns nothing when i use it with the parsed value. :(

following is the sample of the json output (response.write) i get after running the serializer via c#,

Please help me on this one..i cant seem to find the problem here.

EDIT:

if it help, the fiollowing is the json string i get straight from the server witout making any serializations,

Few contents of the question have been removed due to owner request

回答1:

You don't need to use a Json serializer because the remote server already returns a JSON encoded string that you could directly use in your page and avoid the double encoding.

So:

public string GetJson()
{
    // Create Client
    using (WebClient client = new WebClient())
    {
        // Assign Credentials
        client.Credentials = new NetworkCredential("username", "pass");

        // Grab Data
        return client.DownloadString(
            @"www.someurl.com"
        );
    }
}

and then:

<script type="text/javascript">
    var jsons = <%= GetJson() %>;
    function initialize() {
        alert("hahahahaaa");
        document.writeln(jsons.Body[0].RowId.SensorIdValue);
        //document.writeln(myobject.Body[0].RowId.SensorIdValue);
    }
</script>


回答2:

What you're seeing there is doubly JSON-serialized data. You retrieved JSON from the remote server and then JSON encoded it a second time with JavaScriptSerializer. This is a post I wrote about that, in the context of ASMX ScriptServices, which explains in more detail: http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/. Your case isn't exactly the same, but the end result is.

Remove the JavaScriptSerializer code and return the JSON string you retrieved (sjson) instead of serializing it a second time.