locked
Invalid JSON primitive RRS feed

  • Question

  • User-352524747 posted

    I'm trying to decode json below using Json.Decode

    [{"Date":"31.03.2016","EUR":138.77},{"Date":"30.03.2016","EUR":138.65},{"Date":"29.03.2016","EUR":138.51}]

    I get an error System.ArgumentException: Invalid JSON primitive: .

    I tried with this and it works. 

    [{\"Date\":\"31.03.2016\",\"EUR\":138.77},{\"Date\":\"30.03.2016\",\"EUR\":138.65},{\"Date\":\"29.03.2016\",\"EUR\":138.51}]

    The problem is that the resource encode the json without \ (backslash). What should i do to decode it?

    Thursday, March 31, 2016 10:18 PM

Answers

  • User-286291038 posted

    Hi,

    Can you please try Json.Decode sending  JSON.stringify(jsonString) as input.

    Reference: http://stackoverflow.com/questions/17873926/invalid-json-primitive-error

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 31, 2016 11:41 PM
  • User-2057865890 posted

    Hi dow7,

    Try JSON.stringify.

    var s='[{"Date":"31.03.2016","EUR":138.77},{"Date":"30.03.2016","EUR":138.65},{"Date":"29.03.2016","EUR":138.51}]'; 
    JSON.stringify(s);

    Best Regards,

    Chris Zhao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 1, 2016 1:52 AM

All replies

  • User-286291038 posted

    Hi,

    Can you please try Json.Decode sending  JSON.stringify(jsonString) as input.

    Reference: http://stackoverflow.com/questions/17873926/invalid-json-primitive-error

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 31, 2016 11:41 PM
  • User-2057865890 posted

    Hi dow7,

    Try JSON.stringify.

    var s='[{"Date":"31.03.2016","EUR":138.77},{"Date":"30.03.2016","EUR":138.65},{"Date":"29.03.2016","EUR":138.51}]'; 
    JSON.stringify(s);

    Best Regards,

    Chris Zhao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 1, 2016 1:52 AM
  • User-352524747 posted

    I saw that the problem was that the source page requires authenticated user. If i remove WebSecurity.RequireAuthenticatedUser(); from the page that encodes json, my other page that decode works fine.

    I don't understand why it requires me to sing in if i'm already singed in??

    @using System.Globalization;
    @using System.Text.RegularExpressions;
    @{
        Layout = null;
    WebSecurity.RequireAuthenticatedUser();
    var db = Database.Open((string)App.Database); var mx = !Request.QueryString["m"].IsEmpty() ?
    Regex.Replace(Request.QueryString["m"], @"[^a-z,]", string.Empty, RegexOptions.IgnoreCase) : null; if (mx == null) { Response.Redirect("/"); } else { var sql = "SELECT FORMAT (Date,'dd.MM.yyyy') AS Date, {0} FROM FxC WHERE CAST([Date] AS DATE)
    ORDER BY Date DESC"; var result = db.Query(String.Format(sql, mx.ToUpper())).ToArray(); Response.Buffer = true; Response.Clear(); Response.ContentType = "application/json"; Json.Write(result, Response.Output); Response.End(); } }

    @{
        var fxstring = new WebClient().DownloadString("localhost:12274/json?m=eur");
        var elements = Json.Decode(fxstring);
    }
    
    @foreach (var element in elements)
    {
        <p>
            <strong>@element.Date</strong><br>
            @element.EUR
        </p>
    }

    Friday, April 1, 2016 4:58 PM