0

I have a result as JSON string

[{"GROUP_MOD_ID":"G02","ADD":1,"EDIT":0,"DEL":1,"VIEW":null,"UP":1,"PRINT":null,"ADMIN":null,"MOD":null,"ADD-AD":null,"EDIT-AD":null,"DEL-AD":null,"VIEW-AD":null}]

Ok, I want to get name and value do something as

 public static bool Check(string group_mod_id)
    { 
         var obj = GroupModBus.GetGroupModFunc(group_mod_id);
         if (obj != "[]")
         {
            if (obj.Contains["ADD"]=="1")
            {
               //do something
                return true;
            }
         }
         else
             return false;
    }

It's get error Error 1 Cannot apply indexing with [] to an expression of type 'method group'

NOTEs: In here dynamic data, not in a instance.Normally, if data in one instance, I can use like this

public class EXAMPLE
{
    public string ADD { get;set;}
    public string EDIT {get;set;}
    ///etc....
} 
///using  as 
if (obj.ADD =="1")
{
       //do something
       return true;
}

I think i should convert JSON string to array , then get name and get value .Do you think so ?

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Headshot
  • 423
  • 1
  • 6
  • 22
  • 1
    Why is this being done "by hand"? JSON is a serialization format which *should generally **not** be modified / accessed as text* - use (one of the many) libraries available to deal with it as an object graph. – user2864740 May 25 '15 at 02:53
  • Please notes, data is dynamic , not in a instance so i use as follow. – Headshot May 25 '15 at 02:55
  • 1
    That's fine, use a library that allows derserilization to a Dictionay<..> or a JSONObject. – user2864740 May 25 '15 at 02:56
  • Why not use JSON.NET. http://www.newtonsoft.com/json – KrishnaDhungana May 25 '15 at 02:58
  • At this point there is no clear problem in the post except "how to parse JSON" - which has quite good answer on SO already. Feel free to clarify your question why it is not duplicate or ask new question if some particular aspects of Json.Net are not clear (or if you have questions about another JSON parsing library). – Alexei Levenkov May 25 '15 at 03:07
  • I use `dynamic stuff = JsonConvert.DeserializeObject(obj); string add = stuff.ADD; if (add == "1") { //do something return true; }` But it's not work, error `'Newtonsoft.Json.Linq.JArray' does not contain a definition for 'ADD'` – Headshot May 25 '15 at 03:33
  • That's because DerserializeObject returns a JObject/JArray (see the documentation) and *not* a dynamic object *of* the data (the placement of `dynamic` in the line does *not* change the result of the method!). To use a 'dynamic object' instead of JArray/JObject access, see http://stackoverflow.com/questions/3142495 which explains the correct method to use and provides example code. – user2864740 May 25 '15 at 03:36
  • Well,I seen document `http://www.newtonsoft.com/json/help/html/QueryJsonDynamic.htm` and try but it's not work. It's error `Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1` . I use `using Newtonsoft.Json.Linq` – Headshot May 25 '15 at 04:12

0 Answers0