如何将我的c#数组转换为json?

时间:2017-03-07 03:17:48

标签: c# json listview http xamarin.android

我正在尝试将json用于我的移动应用,将文件上传到文件托管网站,然后使用http检索它。现在我遇到了问题,因为我试图在列表视图中传递它并且它不会检索任何数据。我已经在jsontoc#中测试了它但是我仍然认为我的json有问题..你能告诉我,如果我做得对,或者你能告诉我它有什么问题吗?

我的json

[
{
"Mountains": [
  {
    "MtName": "TALAMITAM",
    "Masl":  630,
    "Difficulty": 3,
    "Island":  1
    },
    {
    "MtName": "ALTO PEAK",
    "Masl":  1332,
    "Difficulty": 6,
    "Island": 2
    },
    {
    "MtName": "CANDALAGA",
    "Masl":  1232,
    "Difficulty": 7,
    "Island": 3
    }       
  ]
} 
]

这是我在c#中的数组

 public static List<Mountain> MountainList = new List<Mountain>()
        {

            new Mountain()
            {
                MtName = "ALTO PEAK",
                Masl = 1332,
                Difficulty = 6,
                Island = 2
            },
            new Mountain()
            {
                MtName = "APO",
                Masl = 2956,
                Difficulty = 7,
                Island = 3                   
            },
            new Mountain()
            {
                MtName = "CANDALAGA",
                Masl = 1232,
                Difficulty = 7,
                Island = 3                   
            },              
        } 

2 个答案:

答案 0 :(得分:1)

您的JSON具有以下格式:

var ro = new Rootobject();
// more code here to populate the ro properties
var json = JsonConvert.SerializeObject(ro);

您有一个UIView数组,因此它不符合格式。尝试使用上述格式,然后使用Json.NET将您的类转换为JSON,如下所示:

import UIKit

class MyView: UIView {}

class Controller: UIViewController {

    lazy var myView = MyView()
}

答案 1 :(得分:0)

Mountain类列表的JSON是

[
  {
    "MtName": "TALAMITAM",
    "Masl":  630,
    "Difficulty": 3,
    "Island":  1
  },
  {
    "MtName": "ALTO PEAK",
    "Masl":  1332,
    "Difficulty": 6,
    "Island": 2
  },
  {
    "MtName": "CANDALAGA",
    "Masl":  1232,
    "Difficulty": 7,
    "Island": 3
  }       
]

您拥有的JSON是一个包含属性public List<Mountain> MountainList;

的类的数组