0

I am working in VS 2015 and c#.

I have a Json String which has a list of collections, each collection represents an object,

string wsjson = 
"{
"customAttributes":
    [{"description":"xxxxxxx","id":11,"value":"xxxxxxx"},{"description":"xxxxxxx","id":10,"value":"xxxxxxx"}],
"location":{"account":"xxxxxxx","cabinet":"xxxxxxx"},
"misc":{"approved":false,"archived":false,"deleted":false,"echo":true,"external":false,"favorite":false,"officialLocked":false,"signed":false},
"permissions":[{"xxxxxxx":true,"xxxxxxx":false,"edit":true,"noAccess":false,"share":true,"view":true}],
"standardAttributes":{"aclStatus":"xxxxxxx","created":"\/Date(xxxxxxx)\/","createdBy":"xxxxxxx","createdByGuid":"xxxxxxx","envId":"xxxxxxx","extension":"ndws","id":"xxxxxxx","modified":"\/Date(xxxxxxx)\/","modifiedBy":"xxxxxxx","modifiedByGuid":"xxxxxxx","name":"xxxxxxx","officialVer":1,"size":4,"syncMod":xxxxxxx,"url":"xxxxxxx","versions":1}}"

DataSet wsdataSet = JsonConvert.DeserializeObject<DataSet>(wsjson);

I am getting an error. I tried to follow this (Deserializing Json String into multiple Object types) solution but I am getting error for this line as my jason data is in a string and no function to parse string.

var j = JArray.Parse(data);

Here is the visual image of the jason data.

enter image description here

Actual code block in my program is:

foreach (DataRow row in dataTable.Rows)
                {
                    string wsjson = GetWorkspaceProfile(row[0].ToString());                   
                    DataSet wsdataSet = JsonConvert.DeserializeObject<DataSet>(wsjson);
                    DataTable wsdataTable = wsdataSet.Tables["standardAttributes"];
                    foreach (DataRow wsrow in wsdataTable.Rows)
                    {
                        cmbWorkspaceByCabinet.Items.Add(new KeyValuePair<string, string>(row["envId"].ToString(), wsrow["name"].ToString()));
                    }
                }

Where GetWorkspaceProfile is a string type return function which return me JSON data as string like the image above.

public string GetWorkspaceProfile(string WorkspaceId)
        {
            string responseStr = "";
            string url = "v1/Workspace/" + WorkspaceId + "/info";

            RestType type = RestType.GET;
            Boolean useXml = false;
            RestRequest rr = FormRequest(type, url, useXml);
            IRestResponse response;
            try
            {
                response = executeRequest(rr);
                responseStr = response.Content;
            }
            catch (Exception ex)
            {

                return null;
            }
            return responseStr;
        }
2
  • Unless they’ve changed their string lexing and parsing rules, that snippet doesn’t look like valid C#. Commented Sep 28, 2017 at 14:19
  • Hi Daniel: I just added the actual code block if you can help. thanks in advance. Commented Sep 28, 2017 at 15:32

1 Answer 1

1

JArray.Parse will not work, because you don't have a json array, it is an object. Also not the all values of that object are collections, for example location is also object, not a collection. You have some options to parse it

  1. Parse root object into Dictionary

    JsonConvert.DeserializeObject<Dictionary<string, string>>(wsjson)
    

    then parse every value of the dictionary to array if value is array and to dictionary if value is object.

  2. Create a C# class according to your json data and parse string directly into instance of that class

    JsonConvert.DeserializeObject<JsonModel>(wsjson);
    

    where JsonModel is the class you need to create.

  3. You can use JArray and JToken to get the values you want using json path.

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

3 Comments

Hi Ruben, I just tried option 1 and getting this error: Unexpected character encountered while parsing value: [. Path 'customAttributes', line 1, position 21. this is how I used it: Dictionary<string, string> j = JsonConvert.DeserializeObject<Dictionary<string, string>>(wsjson); Can you please explain it little more if possible. thanks
for option 2: Would you be able to send me a sample class based on my scenario and how to use the class. thanks in advance.
@user1747541 try this stie json2csharp.com to generate c# model

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.