1

I'm trying to get an array out of some JSON code.
Which I get from here: JSON code source

I have this, but I have no idea how to make the output usable.

        //Some other code above this line
        var jsonout = new JavaScriptSerializer().Deserialize<List<Rootobject>>(json);

    }
}

//JSON structure
public class Rootobject
{
    public Class1[] Property1 { get; set; }
}

public class Class1
{
    public string group { get; set; }
    public string tracker { get; set; }
    public string measureTime { get; set; }
    public int minAgo { get; set; }
    public float lat { get; set; }
    public float lon { get; set; }
    public History[] history { get; set; }
}

public class History
{
    public float lat { get; set; }
    public float lon { get; set; }
    public int minAgo { get; set; }
}

I have no idea how to to get the lat, lon, measureTime, etc.. from the output. Do you guys a nice way on how to do it? (I'm very new with using JSON in C#).

2
  • Do you have to use JavaScriptSerializer rather than (say) Json.NET? What does your current code actually do? (It looks like you should be deserializing a List<Class1>, not a List<Rootobject>.) Commented Aug 17, 2017 at 18:11
  • Your data model is wrong -- the extra level Class1 is unnecessary. Post your JSON to json2csharp.com and you can get a corrected data model, where RootObject has the properties from Class1. Commented Aug 17, 2017 at 18:14

3 Answers 3

1

Your data model is wrong -- the extra level Class1 is unnecessary. Post your JSON to http://json2csharp.com/ and you can get a corrected data model, where RootObject has the properties from Class1:

public class History
{
    public double lat { get; set; }
    public double lon { get; set; }
    public int minAgo { get; set; }
}

public class RootObject
{
    public string group { get; set; }
    public string tracker { get; set; }
    public string measureTime { get; set; }
    public int minAgo { get; set; }
    public double lat { get; set; }
    public double lon { get; set; }
    public List<History> history { get; set; }
}

And then do:

var jsonout = new JavaScriptSerializer().Deserialize<List<RootObject>>(json);
foreach (var root in jsonout)
{
    Console.WriteLine(root.measureTime); // For instance.
    Console.WriteLine(root.lat); // For instance.
    Console.WriteLine(root.lon); // For instance.
}
Sign up to request clarification or add additional context in comments.

Comments

0

You're deserializing the wrong type. You don't have a collection of Rootobject, you have a single Rootobject that contains a collection of Class1.

var jsonout = new JavaScriptSerializer().Deserialize<Rootobject>(json);

At that point just use object notation.

foreach(var thing in jsonout.Property1)
{
    thing.lat;
    thing.lon;
}

Comments

0

You can declare your general classes like this

public class Class1
{
    public string group { get; set; }
    public string tracker { get; set; }
    public string measureTime { get; set; }
    public int minAgo { get; set; }
    public float lat { get; set; }
    public float lon { get; set; }
    public List<History> history { get; set; }

    public List<History> GetListHistories(){
      return history;
    }
}

public class History
{
    public float lat { get; set; }
    public float lon { get; set; }
    public int minAgo { get; set; }
}

And implementation like this

var jsonout = new JavaScriptSerializer().Deserialize<List<Class1>>(json);
foreach(var item in jsonout)
{
  console.Write(item.gruop);
  console.Write(item.tracker);
  // more properties
  // and:
  List<History> list = item.GetListHistories();
  foreach(var l in list)
  {
     console.Write(l.lat);
     console.Write(l.lon);
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.