2013-04-11 203 views
0

好吧,所以我一直被困在JSON.NET之前,我又被卡住了,多亏了我最后一个问题,你们帮助我,我成功地完成了我的项目:3。但是现在我需要反序列化一个并不真正拥有密钥的JSON。那么它只是一个整数,它可以在0 - 50之间变化,这意味着我不知道如何做到这一点。我试图用一个IDictionary但惨遭失败...反正继承人的JSON:用c#解析JSON没有钥匙

{ 
"response": { 
    "success": 1, 
    "current_time": 1362339098, // this will return the time when cache was generated 
    "prices": { 
     "35": { // defindex 
      "11": { // quality 
       "0": { // price index (crate # or unusual effect, 0 when not used) 
        "current": { // current price 
         "currency": "keys", 
         "value": 39, 
         "value_high": 41, 
         "date": 1357515306 // date when the price was updated 
        }, 
        "previous": { // previous price 
         "currency": "keys", 
         "value": 37, 
         "value_high": 39 
        } 
       } 
      }, 
      "3": { // quality 
       "0": { // price index 
        "current": { 
         "currency": "metal", 
         "value": 0.33, 
         "value_high": 0.66 
        } 
       } 
      } 
     }, 
     "5002": { // refined metal, useful for converting usd prices into metal 
      "6": { 
       "0": { 
        "current": { 
         "currency": "usd", 
         "value": 0.39, 
         "value_high": 0.42, 
         "date": 1358090106 
        } 
       } 
      } 
     },       
     "5022": { // one of the crate defindex 
      "6": { 
       "1": { // crate #1 
        "current": { 
         "currency": "metal", 
         "value": 1.33, 
         "value_high": 1.55, 
         "date": 1357515175 
        } 
       } 
      } 
     } 
    } 
} 
} 

(DAT格式... ...再次)

而且继承人我可怜的企图:

public class Json1 { 
    public Json2 response { get; set; } 
} 
public class Json2 { 
    public int success { get; set; } 
    public string current_time { get; set; } 
    public IDictionary<int, Json3> prices { get; set; } 
} 
public class Json3 { 

} 
public class Json4 { 

} 
public class Json5 { 
    public Json6 current { get; set; } 
} 
public class Json6 { 
    public string currency { get; set; } 
    public string value { get; set; } 
    public string value_high { get; set; } 
} 

Json 3和4是空的,因为我一直删除尝试不同的东西...

但是啊...我已经习惯了JSON,但无法弄清楚这一点。任何友好的回复都会提前获得大力赞赏。

(我知道我用了一些彩车和长字符串,这是故意的,但我想是可以改变的)

+0

所以我很困惑,你说JSON可以变化0-50,但是你发布的JSON是5002到5022.这是否意味着总是只有两个条目可以变化50(如此5000到5050)或者这是否意味着你有0-50个数字可以是任何数字? – AlexLordThorsen 2013-04-11 17:42:34

+0

@Rawrgulmuffins你可以看看Defindex,它可以随着时间的变化而变化,之后在0-600之间变化,此后它在0-55之间变化,但将来会升高。我应该对此更加具体。 – LeCoffee 2013-04-11 17:55:19

+0

我的头脑中有一个解决方案是拥有4个嵌套的foreach语句(一个用于defindex,一个用于质量,一个用于价格,然后一个用于当前和以前)。 – AlexLordThorsen 2013-04-11 18:21:24

回答

1

我personnally使用Newtonsoft.Json是非常好的,你可以在http://json.codeplex.com/ 它处理的发现匿名类型和linq之间的许多其他的东西。

还有一个很好的文档。 SI你应该确定:

只是一个简单的例子:

连载:

var listId = new List<int>(); 
listId.Add(1); 
listId.Add(2); 
listId.Add(3); 
String jsonList = JsonConvert.SerializeObject(listId); 

反序列化:

List<int> listID = JsonConvert.DeserializeObject<List<int>>(JsonListOfID); 
+0

对不起,但这不是和JSON.NET一样,只是一种不同的方法?除此之外,我不知道从这种反序列化开始。 – LeCoffee 2013-04-11 18:38:27

+0

谢谢我将这个标记为答案,因为它是最有帮助的,即使它不回答我的问题... – LeCoffee 2013-04-12 15:53:44

+0

@ MrShaunh77我添加了一个答案,应该直接解决您的问题。看一看。 – 2013-04-15 20:25:12

0

如果您的Visual Studio 2012,你可以做编辑 - >粘贴特殊 - >将JSON粘贴为使用JSON生成此类的类。虽然在这种情况下,这可能不会帮助你,因为属性名称是动态的。虽然结构可能会有所帮助。另一个选择可能是将响应解析为JToken,然后使用linq来获取数据。

public class Rootobject 
{ 
public Response response { get; set; } 
} 

public class Response 
{ 
public int success { get; set; } 
public int current_time { get; set; } 
public _35 _35 { get; set; } 
public _5002 _5002 { get; set; } 
public _5022 _5022 { get; set; } 
} 

public class _35 
{ 
public _11 _11 { get; set; } 
public _3 _3 { get; set; } 
} 

public class _11 
{ 
public _0 _0 { get; set; } 
} 

public class _0 
{ 
public Current current { get; set; } 
public Previous previous { get; set; } 
} 

public class Current 
{ 
public string currency { get; set; } 
public int value { get; set; } 
public int value_high { get; set; } 
public int date { get; set; } 
} 

public class Previous 
{ 
public string currency { get; set; } 
public int value { get; set; } 
public int value_high { get; set; } 
} 

public class _3 
{ 
public _01 _0 { get; set; } 
} 

public class _01 
{ 
public Current1 current { get; set; } 
} 

public class Current1 
{ 
public string currency { get; set; } 
public float value { get; set; } 
public float value_high { get; set; } 
} 

public class _5002 
{ 
public _6 _6 { get; set; } 
} 

public class _6 
{ 
public _02 _0 { get; set; } 
} 

public class _02 
{ 
public Current2 current { get; set; } 
} 

public class Current2 
{ 
public string currency { get; set; } 
public float value { get; set; } 
public float value_high { get; set; } 
public int date { get; set; } 
} 

public class _5022 
{ 
public _61 _6 { get; set; } 
} 

public class _61 
{ 
public _1 _1 { get; set; } 
} 

public class _1 
{ 
public Current3 current { get; set; } 
} 

public class Current3 
{ 
public string currency { get; set; } 
public float value { get; set; } 
public float value_high { get; set; } 
public int date { get; set; } 
} 
+0

谢谢,但http://json2csharp.com/更好,这不会像你所说的动态键那样工作。 :/ – LeCoffee 2013-04-11 18:35:15

1

您在正确的轨道上IDictionary。这个JSON结构实际上是一堆嵌套字典。尝试使你这样的类:

public class Json1 
{ 
    public Json2 response { get; set; } 
} 
public class Json2 
{ 
    public int success { get; set; } 
    public string current_time { get; set; } 
    public IDictionary<int, IDictionary<int, IDictionary<int, Json5>>> prices { get; set; } 
} 
public class Json5 
{ 
    public Json6 current { get; set; } 
} 
public class Json6 
{ 
    public string currency { get; set; } 
    public string value { get; set; } 
    public string value_high { get; set; } 
} 

你可以反序列化这样的:

Json1 obj = JsonConvert.DeserializeObject<Json1>(json); 

一旦你反序列化它,你可以在价值观得到这样的:

foreach (KeyValuePair<int, IDictionary<int, IDictionary<int, Json5>>> price in obj.response.prices) 
{ 
    Console.WriteLine("price index: " + price.Key); 
    foreach (KeyValuePair<int, IDictionary<int, Json5>> quality in price.Value) 
    { 
     Console.WriteLine("\t quality: " + quality.Key); 
     foreach (KeyValuePair<int, Json5> index in quality.Value) 
     { 
      Console.WriteLine("\t\t index: " + index.Key); 
      Console.WriteLine("\t\t\t currency: " + index.Value.current.currency); 
      Console.WriteLine("\t\t\t value: " + index.Value.current.value); 
      Console.WriteLine("\t\t\t value_high: " + index.Value.current.value_high); 
     } 
    } 
} 

以上的输出(表明它的工作原理):

price index: 35 
     quality: 11 
       index: 0 
         currency: keys 
         value: 39 
         value_high: 41 
     quality: 3 
       index: 0 
         currency: metal 
         value: 0.33 
         value_high: 0.66 
price index: 5002 
     quality: 6 
       index: 0 
         currency: usd 
         value: 0.39 
         value_high: 0.42 
price index: 5022 
     quality: 6 
       index: 1 
         currency: metal 
         value: 1.33 
         value_high: 1.55