locked
Converting scientific double value to string while parsing using Windows.Data.Json RRS feed

  • Question

  • I am developing WP 8.1 app. I have a JSON which gives me a longer value as one of response parameter. JSON structure is given below. When I use Windows.Data.Json, I have to use GetNumber() method to retrieve it, but it returns me in form of scientific notation. For ex. the response contains value 1070175340166619100, it returns me 1.07017534016662E+18. How can I get complete value as string. My efforts are given below.

    {
        "data": {
            "records": [
                {
                    "channel": "foo",
                    "channelId": 1070175340166619100
                },
                {
                    "channel": "bar",
                    "channelId": 5
                }
            ]
        }
    }

    var JsonObj = JsonObject.Parse(json);
    var array = JsonObj["data"].GetObject()["records"].GetArray();
    
    var ChannelDictionary = new Dictionary<string, string>();
    
    foreach (var item in array)
    {
        ChannelDictionary.Add(item.GetObject()["channelId"].GetNumber().ToString(), item.GetObject()["channel"].GetString());
    }


    • Edited by Xyroid Tuesday, May 27, 2014 1:31 PM
    Tuesday, May 27, 2014 1:30 PM

Answers

  • Ok, there should be API to get appropriate JSON value e.g. long value. That's why people prefer JSON.NET
    • Marked as answer by Xyroid Thursday, May 29, 2014 5:50 AM
    Thursday, May 29, 2014 5:50 AM

All replies

  • See Standard Numeric Format Strings for information on formatting numbers.

    --Rob

    Tuesday, May 27, 2014 11:47 PM
  • It's not helpful  me. I think you didn't understand my question. I DON'T want it in double, even that id is not a floating point value. I need a string representation.
    Wednesday, May 28, 2014 5:38 AM
  • Have you tried to cast it to a UINT64 or a long first, before calling ToString()?

    Darin R.

    Wednesday, May 28, 2014 1:14 PM
  • JsonObject.GetNumber returns a double. There is no way to change that, so you'll need to convert it after. If your question is about formatting (as you expressed it) then please read the documentation I linked. It explains how to convert a number to a string while specifying the format of the string. By default the Generic format will use scientific notation for large numbers, but you can specify other formats if needed.

    double test = 1070175340166619100;
    Debug.WriteLine(test.ToString("F0"));
    // output: 1070175340166620000
            

    Casting to Int64 gives a different rounding error: 1070175340166619136

    If your question is about precision (are you getting a different value from GetNumber than you see in the original Json) then you'll need to do your own interpretation and conversion from the Json rather than using JsonObject.Parse.

    --Rob

    Wednesday, May 28, 2014 1:36 PM
  • It gives me wrong number.
    Thursday, May 29, 2014 5:48 AM
  • Ok, there should be API to get appropriate JSON value e.g. long value. That's why people prefer JSON.NET
    • Marked as answer by Xyroid Thursday, May 29, 2014 5:50 AM
    Thursday, May 29, 2014 5:50 AM