0

In one of my project i want to parse a JSON array with different key name. For Example

{ "details": [ 
    { "state": "myState1",
      "place": [
        { "name": "placeName" } ] },
    { "state": "myState2",
      "place": [
        { "name1": "placeName" } ] },
    { "state": "myState3", 
      "place": [
        { "name2": "placeName" } ] } }

So in this JSON please look at the place array. each time key like name1,name2,name3..., .And also it is not necessary to get the same JSON at all the time. In some time only state1 or state1 and state3 and in some time state1 to state 50.

So how can i identify and parse exact data from this array

dbc
  • 104,963
  • 20
  • 228
  • 340
Hope
  • 1,252
  • 4
  • 17
  • 35
  • Similar question [here](http://stackoverflow.com/questions/1212344/parse-json-in-c-sharp) I think... – CompanyDroneFromSector7G May 18 '12 at 12:30
  • Can't you replace "name(n)" by just "name"? If this is the case, you have the problem pretty solved. – Claudio Redi May 18 '12 at 12:30
  • I think you're misunderstanding how to use json. You either parse it or you don't. Your json result has to do with how the data is structured and which parser you use. If you want to do get the place names you'll have to query for it, likely using linq on a dictionary to get the values on "place". Do not that the objects inside "place" are not the same (they ahve different keys) and will make your querying much harder. – Thinking Sites May 18 '12 at 12:32
  • Sorry that is not my actual problem. I am using c# code for parsing this jason. for that i am using a class for storing keyvalue. So in that class i want to declare data members for storing these key value. So there data member should have same name(key name). So if the key is different(different valuer) how can i parse that json – Hope May 18 '12 at 12:50
  • Please post your C# code as well. – Joshua Drake May 18 '12 at 12:57

1 Answers1

1

First of all your JSON is not well-formatted. You miss a closing square bracket ] before the last closing curly bracket }.

Then, you can't parse variable-name properties to a static class, but you can turn them into a dictionary. Here's an example of mapping classes that work with variable places:

public class Details
{
    public string state { get; set; }
    public List<Dictionary<string, string>> place { get; set; }
}
public class Wrap
{
    public Details[] details { get; set; }
}

static void Main(string[] args)
{
    string txt = File.ReadAllText("MyJSONFile.txt");
    JavaScriptSerializer ser = new JavaScriptSerializer();
    var data = ser.Deserialize<Wrap>(txt);
}

If also the place property will change name, I think the simplest way to parse it is to use the following, very loosely typed, class:

public class Wrap
{
   public List<Dictionary<string,object>> details { get; set; }
}

where the object in the dictionary will be a string or a dictionary of properties according to the values in the JSON.

digEmAll
  • 56,430
  • 9
  • 115
  • 140
  • thanks for your answer. Is it possible to count details of the records in data – Hope May 21 '12 at 05:36
  • Sorry, I don't think I got your question... isn't details.Count enough ? – digEmAll May 21 '12 at 07:09
  • Sorry. Not only for count but i want to read each and every values in the place list. So for that first read count and based on that count use a loop to read all the values inside the list. – Hope May 21 '12 at 08:35
  • @prasobhTK: My suggestion is to have a look at the deserialized object using the debug watch. Once you understand the resulting structure, iterating over the list/dictionaries inside it, should not be a problem... – digEmAll May 21 '12 at 10:52