1

Hey all I am looking at the following json string I Serialize using SerializeObject in Json.net:

{
  "ServTypeList": [
    {
      "id": 1,
      "name": "COMPUTER"
    },
    {
      "id": 2,
      "name": "MONITOR"
    },
    {
      "id": 3,
      "name": "NETWORK COMPONENTS"
    },
    {
      "id": 4,
      "name": "OFFICE EQUIPMENT"
    },
    {
      "id": 5,
      "name": "SOFTWARE"
    },
    {
      "id": 6,
      "name": "STORAGE"
    }
  ],
  "ServTypeList1": [
    {
      "CurrentVersion": "Jun 23 2017 (0 days ago)",
      "theQuery": "SELECT id,name\tFROM servType"
    }
  ]
}

And this is my C# code in order to format it like above:

string json = JsonConvert.SerializeObject(_ServTypeList, Formatting.Indented);

The _ServTypeList is in a DataSet format which has a name of ServTypeList.

So now I am wanting to take out the TypeList and just get teh array of data from the json string.

So I tried something like this:

var trying = json["TypeList"];

But that gives me an error of

Error CS1503 Argument 1: cannot convert from 'string' to 'int'

So I'm not sure why its trying to cast it as an int?

How do I just get this information:

[{
      "id": 1,
      "name": "COMPUTER"
    },
    {
      "id": 2,
      "name": "MONITOR"
    },
    {
      "id": 3,
      "name": "NETWORK COMPONENTS"
    },
    {
      "id": 4,
      "name": "OFFICE EQUIPMENT"
    },
    {
      "id": 5,
      "name": "SOFTWARE"
    },
    {
      "id": 6,
      "name": "STORAGE"
}]
1
  • Instead of serializing the whole DataSet, just serialize the table you need, as suggested by @Elemental Pete: string json = JsonConvert.SerializeObject(_ServTypeList.Tables["ServTypeList"], Formatting.Indented); Commented Jun 24, 2017 at 14:13

2 Answers 2

2

Your object json is of type string, therefore when you try to do json["TypeList"], it expects an int because that's how you get a single char at the specified index.

For example, if you have string foo = "abc", then foo[0] will return 'a'. You can't be passing a string like "TypeList" to that.

There are a few ways to achieve what you want; a regex would work well, grouping everything in between [] sets.


This regex should give you what you want. Simply use it this way:

var pattern = @".*\[([\s\S]*)\],.*";
string servTypeList = Regex.Match(json, pattern).Groups[1].Value;
Sign up to request clarification or add additional context in comments.

Comments

2

In general, you can use the [JsonIgnore] attribute on public properties in your object class to prevent them from being serialized in the first place. Then you don't need to worry about removing them later. For example, to serialize a list of program users, your user class might look like this (for demonstration purposes only - obviously this would not be the most secure way to handle a password):

public string DisplayName { get; set; }
public string UserName { get; set; }
public string EmailAddress { get; set; }
[JsonIgnore]
public string Password { get; set; }

In this case, since you are serializing a DataSet, it appears that it contains two tables, one of which you are trying to ignore? Can you just serialize the DataTable that you want to keep?

string json = JsonConvert.SerializeObject(_ServTypeList.Tables[0], Formatting.Indented);

If not, the previous answer looks good to me, but you are introducing inefficiency throughout the process - first to serialize unneeded info, then to presumably transfer it somewhere, and then to filter it out on the other end.

The following test code:

DataSet ds = new DataSet();
DataTable dt1 = new DataTable();
dt1.Columns.Add("id", typeof(int));
dt1.Columns.Add("name", typeof(string));

ds.Tables.Add(dt1);

for (int i = 0; i < 10; i++)
{
    DataRow dr = dt1.NewRow();
    dr["id"] = i;
    dr["name"] = "Item " + i.ToString();
    dt1.Rows.Add(dr);
}

string s = JsonConvert.SerializeObject(ds.Tables[0]);
Debug.WriteLine(s);

Yields this JSON:

[{"id":0,"name":"Item 0"},{"id":1,"name":"Item 1"},{"id":2,"name":"Item 2"},{"id":3,"name":"Item 3"},{"id":4,"name":"Item 4"},{"id":5,"name":"Item 5"},{"id":6,"name":"Item 6"},{"id":7,"name":"Item 7"},{"id":8,"name":"Item 8"},{"id":9,"name":"Item 9"}]

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.