-3

I am trying to loop through the object I received from jQuery AJAX method into my aspx.cs page.

My object structure (I get object[] of 5 objects)

I get object[] of 5 objects

I want to get the value of BusinessOwner (how to access these properties)

how to access these properties

1

2 Answers 2

0

Maybe something like this might help?

using System;
using System.Dynamic;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;

public class Program
{
    public static void Main()
    {
        IDictionary<string,object> rptBusDetails = new ExpandoObject();
        rptBusDetails["rptBusDetails"] = new List<object> 
        {
            new Dictionary<string, string>() {{"BusinessOwner", "Mark"}, {"ChartReq", ""}}, 
            new Dictionary<string, string>() {{"BusinessOwner", "Tom"}, {"ChartReq", ""}}
        };
        var parent = new object[] { rptBusDetails };

        foreach(var node in parent)
        {
            var details = JObject.FromObject(node);
            foreach(var detail in details["rptBusDetails"])
            {
                string owner = detail["BusinessOwner"].Value<string>();
                Console.WriteLine(owner);
            }           
        }
    }
}

[Updated]

Sign up to request clarification or add additional context in comments.

7 Comments

thanks for your reply, Itried your way, but it shows cannot convert object to array at "foreach (var thing in (Array)childNode)" What I want is I am getting the object from my ajax post method in client side, I am getting and object array, which has json fields. I want to get the values of the fields. Below is my json string that is being formed as passed as object { rptBusDetails: [ [{ "BusinessOwner": "mark", "ChartReq": "", "ColorReq": "", "EmailList": "", "ReportApplication": "", "ReportAud": "", "ReportDelivery": "", "ReportDesc": "" }], }
Try converting that object to a JObject (instead of an array) using JObject.Parse (newtonsoft.com/json/help/html/…) and then access properties via stackoverflow.com/a/9010085/1162991
no luck Unable to cast object of type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]' to type 'Newtonsoft.Json.Linq.JObject'.
How about JObject o = JObject.FromObject(object); newtonsoft.com/json/help/html/CreatingLINQtoJSON.htm?
worked like charm, thanks a lot but how do I get the key and value from the JObject ?
|
0

What have you tried? something as easy as this should work, based on your question.

List<string> businessOwners = new List<string>();
foreach (object[] objArray in rptBusDetails) {
    foreach (object obj in objArray){
        businessOwners.Add((JToken)obj["BusinessOwner"].ToString());
    }
}

since you didn't say what you wanted to do with the BusinessOwner property, I put them in a list.

6 Comments

Hi Andrew, thanks for your reply, I want to get the value of Business Owner that is "Mark" I tried your solution but it says "cannot apply indexing of type object"
@Sandy Right you're using a dynamic object when you really should be using JTokens, try casting the obj as a JToken. Why are you using object as a type anyways?
I am getting the object from my ajax post method in client side, I am getting and object array, which has json fields. I want to get the values of the fields. Below is my json string that is being formed as passed as object { rptBusDetails: [ [{ "BusinessOwner": "mark", "ChartReq": "", "ColorReq": "", "EmailList": "", "ReportApplication": "", "ReportAud": "", "ReportDelivery": "", "ReportDesc": "", "ReportDisplay": "", "ReportFormat": "", "ReportFreq": "", "ReportFreqDesc": "", "ReportTitle": "" }], }
@Sandy Oh sorry I missed one of the levels you had! I didn't realize it was an array of arrays of JTokens. I corrected it now.
I tried again var tempobj = (JToken)(obj[0])["BusinessOwner"]; Cannot apply indexing to object. Should I try post the data in any other format than object, I am not sure how to ? I have array of objects in my client side which I am trying to get into my webmethod in code behind
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.