0

I'm having a problem trying to deserialize this json array and trying to use the variables. here is the json array that I'm trying to use

 [ {
 "Room": [   
  {     
    "ID": 347,   
    "Name": "Beach Villas (68 SQM)",   
    "Description": "Nestled amidst the vegetation along the pristine white beach, the elegantly designed Beach Villas feature natural wood & stone flooring, open-air bathroom, private outdoor setting with direct access to the beach. ",   
         "FileImageUrl": "http://url.com?filedataid=160"
  }   
],   
   "RoomFacility": [   
  {   
    "Facility": "Air conditioning"   
  },  
  {
    "Facility": "Coffee/tea/espresso making facilities"   
  },   
  {   
    "Facility": "Private Deck"   
  },   
  {
    "Facility": "Mini Bar"   
  },
  {
    "Facility": "Internet Access"
  },
  {
    "Facility": "Ceiling fan"   
  },   
  {
    "Facility": "Complimentary internet access"    
  },   
  {
    "Facility": "CD/DVD player with home theater system and satellite TV"   
  },   
  {
    "Facility": "Outdoor Bathroom and Jacuzzi Bathtub"   
  },   
  {    
    "Facility": "Safety Box"    
  }     
]  }]

c# code to retreive parse JSON arry

    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(requestUri);
    httpWebRequest.Method = WebRequestMethods.Http.Get;
    httpWebRequest.Accept = "application/json";

    var response = (HttpWebResponse)httpWebRequest.GetResponse();

    string text;
    using (var sr = new StreamReader(response.GetResponseStream()))
    {
        text = sr.ReadToEnd();
    }

    StringBuilder sb = new StringBuilder();
    dynamic stuff = JsonConvert.DeserializeObject<object>(text);

    string roomID = stuff[0]["Room"]["ID"]; 
    ///this line gives me error

from this I'm trying to retrive values using the dynamic variable.. I tried something like the last line

ERROR

Accessed JArray values with invalid key value: "ID". Array position index expected. any help would be appreciated?

11
  • So what's the question? Commented Apr 18, 2014 at 13:29
  • Adding your incorrect C# code would really help here. Commented Apr 18, 2014 at 13:30
  • HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(requestUri); httpWebRequest.Method = WebRequestMethods.Http.Get; httpWebRequest.Accept = "application/json"; var response = (HttpWebResponse)httpWebRequest.GetResponse(); using (var sr = new StreamReader(response.GetResponseStream())) { text = sr.ReadToEnd(); } //till here ok dynamic stuff = JsonConvert.DeserializeObject<object>(text); JObject jObject = JObject.Parse(stuff); //here I'm getting error var array = new JArray(jObject["Room"].Values()); Commented Apr 18, 2014 at 13:44
  • var array = new JArray(jObject["Room"].Values()); Commented Apr 18, 2014 at 13:44
  • Do you mind adding that to your question? It's a bit hard to read down here. Commented Apr 18, 2014 at 13:48

2 Answers 2

0

This question mentions a possible solution

Change

string roomID = stuff[0]["Room"]["ID"];

With

string roomID = stuff[0]["Room"][0]["ID"];
Sign up to request clarification or add additional context in comments.

4 Comments

but could you tell me how to iterate throught Room and RoomFacility objects in case Json array is more lengthy
@user3363268 I'm going to let you in on a secret. I've never retrieved a json array in C# before. All I did was google the error.
@user3363268 This has a possible way to iterate through the Json array. stackoverflow.com/questions/11132288/…
Oh! Even I did google search, I guess I'm not at all good with keywords.. anyway thanks
0

stuff[0]["Room"] is an array, tray this line:

string roomID = stuff[0]["Room"][0]["ID"];

6 Comments

could you suggest me how to iterate throught both the objects in case the total count of 'stuff' object is 6
okay, In this case I've two objects of Room and RoomFacilty which is wrapped with outer Obejct. so its not very straight forward as the link you had send. Could you suggest me some tips and trick when dealing with complex json array
If you have a .length property in the array, you can iterate through each list. And i think that you can use foreach() too.
I suggest you to use Linq in order to work with that kind of arrays.
thanks @Rangar. there is a length property. Can you send me some tips on how to work linq with json array
|

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.