0

Below is parsed json data which is of type string.

  "data": [
    {
      "Company": {
        "id": "1",
        “Value": “20”,
        "companyId": "2001”,
      }
    },
    {
      "Company": {
        "id": "2",
        “value”: "20”,
        "companyId”: "2002”,
      }
    },
    {
      "Company": {
        "id": “3”,
        “value”: “30”,
        "companyId”: "2003”,
      }
    },
 ]

var parseData = Newtonsoft.Json.Linq.JObject.Parse (e.ResponseData.ToString ());

Convert json data string store in array, Arraylist of stored Company Value. this is first time dealing with Json object string.

1 Answer 1

5
string json = @"{
      'status_code': 200,
      'status_text': 'matches found',
      'data': [{
         'company': {
           'id': '1',
           'value': '20',
           'companyId': '2001',}
         },
         {
         'company': {
           'id': '2',
           'value': '20',
           'companyId': '2002',}
         },
         {
         'company': {
           'id': '3',
           'value': '30',
           'companyId': '2003',}
         },]
       }";

JObject jObj = JObject.Parse(json);
var ids = jObj["data"].Children()["company"]["companyId"];
var list = new List<string>();
list.AddRange(ids.Select(id => id.Value<string>()));

foreach (var item in list)
    Console.WriteLine(item);

// Outputs ->
//  2001
//  2002
//  2003

Edit:

"Everything" from companies as a list:

JObject jObj = JObject.Parse(json);
var jEnum = jObj["data"].Children()["company"];

var list = jEnum.Select(company => 
    company.Values().Select(current => 
        current.Value<string>()).ToList()).ToList();
Sign up to request clarification or add additional context in comments.

9 Comments

its Say No overload method takes 0 arugments at list.AddRange(ids.Select(id => id.Value<string>()));
Above works just fine with latest JSON.net Nuget package. Debug.
Console.WriteLine ("ids Value {0}",ids); Newtonsoft.Json.Linq.JEnumerable`1[[Newtonsoft.Json.Linq.JToken, Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=null]] its gives output.
Yes, "ids" field in this example is JEnumerable containing JTokens. You'd want to output the values Console.WriteLine("ids Value {0}", string.Join(",", ids.Select(id => id.Value<string>())));`
var companyid = parseData["data"].Children()["company"]["companyId"]; Console.WriteLine("ids Value {0}", string.Join(",", companyid.Select(id => id.Value<string>()))); gives me compiler error no overload method for Value takes '0' argurments
|

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.