user7926701
user7926701

Reputation:

Simple way to extract specific data from string?

Working on a way to extract data downloaded from a

Webclient.Downloadstring();

For this purpose I'm going to use a regular string as an example.

usually the downloaded string is as:

var data = "{\"err\":[],\"msg\":[],\"state\":null," +
                  "rec\":{\"Vin\":\"1ABC2DEF3GH45678\"," +
                  "\"REG\":\"ST-ABC123\",\"int\":\"12345678\"," +
                  "\"Make\":\"GMC\",\"Model\":\"YUKON\"," +
                  "\"Type\":\"SUV\",\"Color\":\"black metallic\",";

The info that I want to get is the Vin, Registration, ID(int), Make, Model, Color etc.

The way I've been approaching this is as follow.

    public class Program
{
    public static void Main(string[] args)
    {


        var data = "{\"err\":[],\"msg\":[],\"state\":null," +
                  "rec\":{\"Vin\":\"1ABC2DEF3GH45678\"," +
                  "\"REG\":\"ST-ABC123\",\"int\":\"12345678\"," +
                  "\"Make\":\"GMC\",\"Model\":\"YUKON\"," +
                  "\"Type\":\"SUV\",\"Color\":\"black metallic\",";

        data = doc.Replace('{', ' ');
        data = doc.Replace('"', ' ');
        data = doc.Replace('}', ' ');
        var sub = data.Split(':', ',');

        for(int i = 0; i < sub.Length; i++)
        {
            sub[i] = sub[i].Trim();

            /*Console.WriteLine("{0} : {1}", i, sub[i]);
            Outputs :
            0 : err
            1 : []
            2 : msg
            3 : []
            4 : state
            5 : null
            6 : rec
            7 : Vin
            8 : 1ABC2DEF3GH45678
            9 : REG
            10 : ST-ABC123
            11 : int
            12 : 12345678
            13 : Make
            14 : GMC
            15 : Model
            16 : YUKON
            17 : Type
            18 : SUV
            19 : Color
            20 : black metallic
            21 : 
            */
        }

        Vehicle vehicle = new Vehicle(sub[8], sub[10], sub[12], sub[14], sub[16], sub[18], sub[20]);
    }
}

public class Vehicle
{
    public string Vin { get; set; }
    public string Registration { get; set; }
    public string ID { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }
    public string VehicleType { get; set; }
    public string Color { get; set; }
    public string Nav { get; set; }
    public string Transmission { get; set; }

    public Vehicle(string _Vin, string _Reg, string _ID, string _Make, string _Model, string _Type, string _Color)
    {
        this.Vin = _Vin;
        this.Registration = _Reg;
        this.ID = _ID;
        this.Make = _Make;
        this.Model = _Model;
        this.VehicleType = _Type;
        this.Color = _Color;
    }
}

Am I approaching this the right way? Is there a simpler way? And as far as the vehicle class would it be fine the way it is?

It works for me, just wanted some input in my code.

Upvotes: 0

Views: 85

Answers (1)

DavidG
DavidG

Reputation: 118957

Assuming you're just missing a few characters from the data you show us, it is really easy to parse this JSON into C# objects. For example, first we start with some classes:

public class Data
{
    public List<string> err { get; set; }
    public List<string> msg { get; set; }
    public string state { get; set; }
    public Rec rec { get; set; }
}

public class Rec
{
    public string Vin { get; set; }
    public string REG { get; set; }
    public string @int { get; set; }
    public string Make { get; set; }
    public string Model { get; set; }
    public string Type { get; set; }
    public string Color { get; set; }
}

Now using Newtonsoft.JSON:

var result = JsonConvert.DeserializeObject<Data>(data);

Note: I am assuming err and msg as arrays of string and state is also a string.

Upvotes: 3

Related Questions