0

I have a problem with deserializing this JSON data due to duplicate property name value and value_raw.

I have attempted to use a List based object to deserialize the values but this only results in the last value being stored in the object. Apart from this, the object resolves correctly.

JSON string:

{
    "prtg-version":"9.2.0.2236",
    "treesize":576,
    "values":
        [{
            "datetime":"29/09/2012 09:45:00 - 09:50:00",
            "datetime_raw":41181.3680555556,
            "value":"49 %",
            "value_raw":48.5000,
            "value":"0 %",
            "value_raw":0.0000,
            "coverage":"100 %",
            "coverage_raw":"0000010000"
        }]
}  

Please note - the JSON string is what i get back from PRTG, so unfortunately i have to work with it in that format :(

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
JGilmartin
  • 8,683
  • 14
  • 66
  • 85
  • Maybe [this question](http://stackoverflow.com/questions/3877526/json-net-newtonsoft-json-two-properties-with-same-name) can help you – sloth Oct 01 '12 at 09:48
  • How you define you *List based object*? – cuongle Oct 01 '12 at 09:51
  • Not sure that's the case here, "value" & "value_raw" cannot be seen as a duplicate property name, unless we are missing information. Can you provide a piece of example code of what you are doing / trying to do? as this is valid json it should deserialize just fine. – Viezevingertjes Oct 01 '12 at 09:52

3 Answers3

1

You can not deserialize that as the string you provided is NOT a valid JSON. By RFC, all attribute names inside one objects should be unique. The only reasonable way to tackle that - rewrite the part of code where this string comes from.

Sergey Kudriavtsev
  • 10,328
  • 4
  • 43
  • 68
  • Actually, the RFC allows such things, but recommends against them. "The names within an object SHOULD be unique." That said, I don't think I've ever seen a) this used in practice and b) a JSON library that handled it other than giving you one value (chosen randomly) back. This is likely the OP's problem. – Thanatos Oct 01 '12 at 10:03
0

can you first serialise the

            "datetime":"29/09/2012 09:45:00 - 09:50:00",
            "datetime_raw":41181.3680555556,
            "value":"49 %",
            "value_raw":48.5000,
            "value":"0 %",
            "value_raw":0.0000,
            "coverage":"100 %",
            "coverage_raw":"0000010000"

into a List of string and then do further process after?

Larry
  • 2,172
  • 2
  • 14
  • 20
-1
"datetime"    :"29/09/2012 09:45:00 - 09:50:00",
"datetime_raw":41181.3680555556,
"value"       :["49 %","0 %"]
"value_raw"   :[48.5000,0.0000]            
"coverage"    :"100 %",
"coverage_raw":"0000010000"

change the value and value_raw as shown above

Aditya
  • 783
  • 1
  • 8
  • 25