0

This is a very noob question, but I have been googling and can't seem to work out the solution myself.
I have created a class that has a number of fields (below). I am grabbing data from a .JSON file.

public class WeatherData
{
    //WeatherDatas
    public string airtemp { get; set; }
    public string apparenttemp { get; set; }
    public string windspeedkph { get; set; }
    public string windgustskph { get; set; }
    public string humidity { get; set; }
    public string dewpoint { get; set; }
    public string deltaT { get; set; }
    public string pressure { get; set; }

    public WeatherData(string json, int index)
    {
        JObject jsonObject = JObject.Parse(json);
        JToken jObbs = jsonObject["observations"];
        JToken jData = jObbs["data"];            
        airtemp = (string)jData[index]["air_temp"];
        apparenttemp = (string)jData[index]["apparent_t"];
        windspeedkph = (string)jData[index]["wind_spd_kmh"];
        windgustskph = (string)jData[index]["gust_kmh"];
        humidity = (string)jData[index]["rel_hum"];
        pressure = (string)jData[index]["press_qnh"];
    }
}

Using the above I can get the "airtemp" from "WeatherData.airtemp". But due to some bells and whistles I want to add later what I really want to do is return not just the airtemp value but a field/property indicating the type of the value. For example something like:

WeatherData.WeatherDatas.airtemp.value & WeatherData.WeatherDatas.airtemp.type

Where .value would be air temp and .type be the string "airtemp".

I just can't seem to work out how to describe to google what I am trying to do.

Ortiga
  • 8,455
  • 5
  • 42
  • 71
LucasF
  • 121
  • 2
  • 8
  • Could you provide a sample JSON? – Ortiga Feb 02 '14 at 08:10
  • 3
    If `weatherData.WeatherData.airtype.type` is *always* going to return "airtemp", what's the benefit of it? And why are you using strings for all these inherently numeric values? – Jon Skeet Feb 02 '14 at 08:13
  • @JonSkeet I think "airtemp" was just a sample, and OP wants `{ 'airtemp' : 0, 'foo': 10 }` to become, for example, `new []{ new KeyValuePair("airtemp", 0), new KeyValuePair("foo", 10) };` – Ortiga Feb 02 '14 at 08:28
  • 1
    @Andre: If that's the aim, then the question is really unclear. (Well, there's little doubt about it - the question *is* unclear, IMO...) – Jon Skeet Feb 02 '14 at 12:36

5 Answers5

1

I think you may want to look into using a dictionary. Dictionaries in c# use Key, Value pairs, so you may wish to create a dictionary of Your key values would be the type, such as airtemp and the value values would be the value such as 32.54.

I recommend looking up this page if you're new to C# and want to learn Dictionaries, or any other cool C# things. http://www.dotnetperls.com/dictionary

Loothelion
  • 368
  • 1
  • 10
1

What you can do here is to use a struct to represent your instance attributes. S.t like this:

public struct Data
{
    public Type type; // if you want a string type (I don't know why) you can use a string type
    public string Value;
}

Then you class will look like this:

public class WeatherData
{
    //WeatherDatas
    public Data airtemp { get; set; }
    ...

    public WeatherData(string json, int index)
    {
        JObject jsonObject = JObject.Parse(json);
        JToken jObbs = jsonObject["observations"];
        JToken jData = jObbs["data"];            
        airtemp.Value = new Data((string)jData[index]["air_temp"], typeof(string));

        ...
    }
}

Note that: If you have too many properties in your class, consider using a class istead of struct.

Arin Ghazarian
  • 5,105
  • 3
  • 23
  • 21
  • Just an observation but is `Type` in the declaration in the `public Type type` a typo... should it be `string` too? – Chris Hammond Feb 02 '14 at 08:18
  • 1
    At the first look I though he want's to use the actual `Type`, but now that I looked again, you're right, he wants a `string` type, I don't know why though. Anyhow I've edited my answer. Tnx for pointing it out. – Arin Ghazarian Feb 02 '14 at 08:29
0

You need to explore types:

Type myType;
Object windy = "strong";
myType = windy.GetType();

Then use typeof() to see what you've got.

Not sure that dictionary is the way forward.

Peter Smith
  • 5,528
  • 8
  • 51
  • 77
0

You can get property name without change your structure:

by example: PropertyUtil<WeatherData>.GetName(x => x.airtemp);

https://stackoverflow.com/a/6043028/440030 (source code)

public static class PropertyUtil<TSource>
{
    public static string GetPropertyName<TResult>(
        Expression<Func<TSource, TResult>> propertyExpression)
    {
       return (propertyExpression.Body as MemberExpression).Member.Name;
    }
}
Community
  • 1
  • 1
Reza ArabQaeni
  • 4,848
  • 27
  • 46
0

You can create your own data type, instead of using string use something else Like;

public class UserString
{
    public Type _type{ get; set; }
    public string value{ get; set; }

    public UserString()
{
   _type = null;
   value = string.Empty;
}
}

public class WeatherData
{
    //WeatherDatas
    public UserString airtemp { get; set; }
    public UserString apparenttemp { get; set; }
    public UserString windspeedkph { get; set; }
    public UserString windgustskph { get; set; }
    public UserString humidity { get; set; }
    public UserString dewpoint { get; set; }
    public UserString deltaT { get; set; }
    public UserString pressure { get; set; }

    public WeatherData(string json, int index)
    {
        JObject jsonObject = JObject.Parse(json);
        JToken jObbs = jsonObject["observations"];
        JToken jData = jObbs["data"];            
        airtemp.value = (string)jData[index]["air_temp"];
//        airtemp._type = typeof(string); //Something like that.
        apparenttemp = (string)jData[index]["apparent_t"];
        windspeedkph = (string)jData[index]["wind_spd_kmh"];
        windgustskph = (string)jData[index]["gust_kmh"];
        humidity = (string)jData[index]["rel_hum"];
        pressure = (string)jData[index]["press_qnh"];
    }
}
KhanZeeshan
  • 1,410
  • 5
  • 23
  • 36