0

How can I parse this JSON result :

{
   "Output": [
      {
         "LGA11aAust.DistanceToBorder": "2587.4",
         "LGA11aAust.LGA_NAME": "Hume (C)",
         "SustainabilityVicZones.DistanceToBorder": "2575.6",
         "SustainabilityVicZones.REBATEZN": "Metropolitan Melbourne",
         "LGA11aAust.Status": "T",
         "SustainabilityVicZones.Status": "T",
         "Status": "T",
         "Status.Code": "",
         "Status.Description": "",
         "user_fields": []
      }
   ]
}

I am aware of JSON.parse method but with that I cannot access the values of the keys as they have a . in them.

E.g.: I cannot use Output[0].Status.Code as it does not treat Status.Code as a whole, it thinks that the Code is under the Status field.

Please help.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162

4 Answers4

1

You could use JSON .NET my favorite JSON serializer and deserializer.

How to use:



public class Output
{
   [JsonProperty(PropertyName="LGA11aAust.DistanceToBorder")]
   public decimal DistanceToBorder {get; set;}

   //All the other properties

}

Usage:

var deserializedObjects = 
JsonConvert.DeserializeObject<List<Output>>(someJsonResult)
Preben Huybrechts
  • 5,853
  • 2
  • 27
  • 63
0

It sounds like a bad idea... but I think you can do it if you escape the periods with a slash:

JSON.parse('{"a\.b":"c"}')

Gives:

Object
    a.b: "c"

(in the Chrome debugger)

You'd then have to use quotes to access your properties, as such:

var x = JSON.parse('{"a\.b":"c"}');
console.log(x["a.b"]);
gerrod
  • 6,119
  • 6
  • 33
  • 45
  • 1
    Not working, I need to access the value directly but I cannot find a solution to that! – Vidish Joglekar Jun 15 '12 at 02:45
  • 1
    As far as I can tell then, you're boned. Why can't you use the propertyName syntax? You could always use a "for/in" loop if you don't know what the properties are called... Perhaps there's another solution to your problem, if you can explain the context a bit? – gerrod Jun 15 '12 at 04:01
  • The thing is with JSON parse, I cant use the format 'key.value' as the key itself has a '.'. So I cant say 'myData.output_port[i].Status.Code' as the asp compiler thinks that 'Code' is a part of 'Status' but actually its one name for the key- 'Status.Code', you can see it in the JSON result I have pasted above. – Vidish Joglekar Jun 15 '12 at 04:23
  • Are you trying to pass this JSON data from your application to your controller? You won't be able to because of the periods - as I said, you'll have to escape them with a slash. But even then, you'll probably need to write a custom model binder to deserialise the string into whatever format you need. – gerrod Jun 15 '12 at 04:53
  • Its actually a result from a webservice returned form someone else, so I cannot do much with the result of the format. But thanks for your comments. If you find any way out please let me know! – Vidish Joglekar Jun 15 '12 at 05:09
  • In that case, could you use JSON.Net to deserialise the values into a Dictionary instead? See: http://stackoverflow.com/questions/1207731/how-can-i-deserialize-json-to-a-simple-dictionarystring-string-in-asp-net – gerrod Jun 15 '12 at 05:58
0

Check this

Dynamic JSON Parser

Renju Vinod
  • 254
  • 1
  • 3
  • 11
0

I found out the answer to my question.

Instead of using a (.) Dot operator, we can directly write the name of the parameter in square brackets.

Eg:- The value of the parameter 'Status.Code' can be accessed as follows-

 Output[0]["Status.Code"]