locked
Create JSON by ignoring a few properties of a class RRS feed

  • Question

  • User-2061267391 posted

    Hi,

    I need urgent help. I would like to create the following structure in JSON using C# in a WebAPI:

    {
    
    "locations": [
    {
    "latitude": 19.0844,
    "longitude": 72.8360
    }, {
    "latitude": 19.0830,
    "longitude": 72.8406
    }, {
    "altitude": 10.0,
    "latitude": 19.0830,
    "longitude": 72.8406,
    "relevantText": "You are near INOX"
    }
    ]
    
    }

    I have created the following class:

    private class location
        {
            public double latitude { get; set; }
            public double longitude { get; set; }
            public double altitude { get; set; }
            public string relevantText { get; set; }
        }

    I am using it as below:

    passList Objpasslist = new passList();
    IList<location> lslocation = new List<location>();
    object setobj;
    
    
    lslocation.Add(new location
            {
                latitude = 19.0844,
                longitude = 72.8360,
            });
            lslocation.Add(new location
            {
                latitude = 19.0830,
                longitude = 72.8406,
            });
            lslocation.Add(new location
            {
                altitude = 10.0,
                latitude = 19.0830,
                longitude = 72.8406,
                relevantText = "You are near INOX"
            });
    
            Objpasslist.locations = lslocation;
    
    
    setobj = Objpasslist;
            JavaScriptSerializer js = new JavaScriptSerializer();
    
            string strJSON = JsonConvert.SerializeObject(setobj, Formatting.Indented);
            strJSON = strJSON.TrimStart('[');
            strJSON = strJSON.TrimEnd(']');

    The output that i currently get is:

    {
    
    "locations": [
    {
    "latitude": 19.0844,
    "longitude": 72.836,
    "altitude": 0.0,
    "relevantText": null
    },
    {
    "latitude": 19.083,
    "longitude": 72.8406,
    "altitude": 0.0,
    "relevantText": null
    },
    {
    "latitude": 19.083,
    "longitude": 72.8406,
    "altitude": 10.0,
    "relevantText": "You are near INOX"
    }
    ]
    
    }


    How to get the desired output in C#?

    Tuesday, March 28, 2017 7:20 AM

Answers

  • User753101303 posted

    Hi,*

    Trry perhaps http://stackoverflow.com/questions/20974811/remove-null-properties-of-a-object-sent-to-json-mvc that talks about an attribute that ignore null values. For the altitude I would likely use int? (nullable int) so distinguish maybe one day between 0 and "no altitude" ). Of course I assume it really make sense to have a different value client and server side (ie undefined vs null for example).

    Edit: see http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonPropertyAttribute.htm for more information (you also have DefaultValueHandling if really needed)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 28, 2017 7:30 AM
  • User-2061267391 posted

    Although its not a good solution, but Replacing the string worked for me. Code is pasted below for anyone who would need a quick temporary solution.

    string strJSON = JsonConvert.SerializeObject(setobj, Formatting.Indented);
    strJSON = strJSON.TrimStart('[');
    strJSON = strJSON.TrimEnd(']');
    strJSON = strJSON.Replace("\\\\", "\\").Replace(ttvalue.ToString() + ",", "[" + ttvalue.ToString() + "],");
    strJSON = strJSON.Replace("\r\n \"altitude\": 0.0,", "");//This is the code to replace the top spacing
    strJSON = strJSON.Replace(",\r\n \"relevantText\": null", "");//This is the code to replace top spacing and top comma




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 30, 2017 10:44 AM

All replies

  • User753101303 posted

    Hi,*

    Trry perhaps http://stackoverflow.com/questions/20974811/remove-null-properties-of-a-object-sent-to-json-mvc that talks about an attribute that ignore null values. For the altitude I would likely use int? (nullable int) so distinguish maybe one day between 0 and "no altitude" ). Of course I assume it really make sense to have a different value client and server side (ie undefined vs null for example).

    Edit: see http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonPropertyAttribute.htm for more information (you also have DefaultValueHandling if really needed)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 28, 2017 7:30 AM
  • User-2061267391 posted

    Although its not a good solution, but Replacing the string worked for me. Code is pasted below for anyone who would need a quick temporary solution.

    string strJSON = JsonConvert.SerializeObject(setobj, Formatting.Indented);
    strJSON = strJSON.TrimStart('[');
    strJSON = strJSON.TrimEnd(']');
    strJSON = strJSON.Replace("\\\\", "\\").Replace(ttvalue.ToString() + ",", "[" + ttvalue.ToString() + "],");
    strJSON = strJSON.Replace("\r\n \"altitude\": 0.0,", "");//This is the code to replace the top spacing
    strJSON = strJSON.Replace(",\r\n \"relevantText\": null", "");//This is the code to replace top spacing and top comma




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 30, 2017 10:44 AM