Xamarin-读取JSON文件并读取值

Xamarin-读取JSON文件并读取值,xamarin,xamarin.ios,json.net,Xamarin,Xamarin.ios,Json.net,各位!!我是Xamarin的新手。我有这个json文件 { "debug":true, "sequence":[ "p1" ], "pages":[ { "pageId":"p1", "type":"seq", "elements":[ { "type":"smallVideo", "width":300, "height":300, "top":0,

各位!!我是Xamarin的新手。我有这个json文件

{
"debug":true,
"sequence":[
  "p1"
],
"pages":[
  {
     "pageId":"p1",
     "type":"seq",
     "elements":[
        {
           "type":"smallVideo",
           "width":300,
           "height":300,
           "top":0,
           "left":0,
           "file":"xxx.mp4"
        }
     ]
  }
],
 "index":[
  {
     "width":300,
     "height":300,
     "top":0,
     "left":0,
     "goTo":"p1"
  }
]
}
这是我的简单代码

using Newtonsoft.Json;

JObject elements = JObject.Parse(File.ReadAllText("elements.json"));
Console.WriteLine(elements);
好的,我可以在输出屏幕上看到整个JSON文件。好的 但我想阅读任何值,就像javascript,示例

elements.debug(true)

元素。页面[0]。页面ID

因此,我需要像在Javascript中一样基于键/路径检索值。有线索吗

1)选项创建反映JSON结构的
对象

public class Element
{
    public string type { get; set; }
    public int width { get; set; }
    public int height { get; set; }
    public int top { get; set; }
    public int left { get; set; }
    public string file { get; set; }
}

public class Page
{
    public string pageId { get; set; }
    public string type { get; set; }
    public List<Element> elements { get; set; }
}

public class Index
{
    public int width { get; set; }
    public int height { get; set; }
    public int top { get; set; }
    public int left { get; set; }
    public string goTo { get; set; }
}

public class MyObject
{
    public bool debug { get; set; }
    public List<string> sequence { get; set; }
    public List<Page> pages { get; set; }
    public List<Index> index { get; set; }
}

MyObject parsed = JsonConvert.DeserializeObject<MyObject>(File.ReadAllText("elements.json"));

var debug = parsed.debug;
1)选项创建反映JSON结构的
对象

public class Element
{
    public string type { get; set; }
    public int width { get; set; }
    public int height { get; set; }
    public int top { get; set; }
    public int left { get; set; }
    public string file { get; set; }
}

public class Page
{
    public string pageId { get; set; }
    public string type { get; set; }
    public List<Element> elements { get; set; }
}

public class Index
{
    public int width { get; set; }
    public int height { get; set; }
    public int top { get; set; }
    public int left { get; set; }
    public string goTo { get; set; }
}

public class MyObject
{
    public bool debug { get; set; }
    public List<string> sequence { get; set; }
    public List<Page> pages { get; set; }
    public List<Index> index { get; set; }
}

MyObject parsed = JsonConvert.DeserializeObject<MyObject>(File.ReadAllText("elements.json"));

var debug = parsed.debug;
C#与js有点不同,这里需要声明对象。 在您的情况下,您需要创建名为ElementsObj的新类,并且您的对象将是该类实例:

public class ElementsObj
{
    public bool debug { get; set; }
    public List<string> sequence { get; set; }
    public List<Page> pages { get; set; }
    public List<Index> index { get; set; }
}

public class Element
{
    public string type { get; set; }
    public int width { get; set; }
    public int height { get; set; }
    public int top { get; set; }
    public int left { get; set; }
    public string file { get; set; }
}

public class Page
{
    public string pageId { get; set; }
    public string type { get; set; }
    public List<Element> elements { get; set; }
}

public class Index
{
    public int width { get; set; }
    public int height { get; set; }
    public int top { get; set; }
    public int left { get; set; }
    public string goTo { get; set; }
}
公共类元素sobj
{
公共bool调试{get;set;}
公共列表序列{get;set;}
公共列表页{get;set;}
公共列表索引{get;set;}
}
公共类元素
{
公共字符串类型{get;set;}
公共整数宽度{get;set;}
公共整数高度{get;set;}
公共整数top{get;set;}
公共整型左{get;set;}
公共字符串文件{get;set;}
}
公共类页面
{
公共字符串pageId{get;set;}
公共字符串类型{get;set;}
公共列表元素{get;set;}
}
公共类索引
{
公共整数宽度{get;set;}
公共整数高度{get;set;}
公共整数top{get;set;}
公共整型左{get;set;}
公共字符串goTo{get;set;}
}
将来用于从JSON文件生成类

稍后,您可以将JSON反序列化到此对象。 我建议Newtonsoft lib这样做:

ElementsObj tmp = JsonConvert.DeserializeObject<ElementsObj>(jsonString);
ElementsObj tmp=JsonConvert.DeserializeObject(jsonString);
C#与js有点不同,这里需要声明对象。 在您的情况下,您需要创建名为ElementsObj的新类,并且您的对象将是该类实例:

public class ElementsObj
{
    public bool debug { get; set; }
    public List<string> sequence { get; set; }
    public List<Page> pages { get; set; }
    public List<Index> index { get; set; }
}

public class Element
{
    public string type { get; set; }
    public int width { get; set; }
    public int height { get; set; }
    public int top { get; set; }
    public int left { get; set; }
    public string file { get; set; }
}

public class Page
{
    public string pageId { get; set; }
    public string type { get; set; }
    public List<Element> elements { get; set; }
}

public class Index
{
    public int width { get; set; }
    public int height { get; set; }
    public int top { get; set; }
    public int left { get; set; }
    public string goTo { get; set; }
}
公共类元素sobj
{
公共bool调试{get;set;}
公共列表序列{get;set;}
公共列表页{get;set;}
公共列表索引{get;set;}
}
公共类元素
{
公共字符串类型{get;set;}
公共整数宽度{get;set;}
公共整数高度{get;set;}
公共整数top{get;set;}
公共整型左{get;set;}
公共字符串文件{get;set;}
}
公共类页面
{
公共字符串pageId{get;set;}
公共字符串类型{get;set;}
公共列表元素{get;set;}
}
公共类索引
{
公共整数宽度{get;set;}
公共整数高度{get;set;}
公共整数top{get;set;}
公共整型左{get;set;}
公共字符串goTo{get;set;}
}
将来用于从JSON文件生成类

稍后,您可以将JSON反序列化到此对象。 我建议Newtonsoft lib这样做:

ElementsObj tmp = JsonConvert.DeserializeObject<ElementsObj>(jsonString);
ElementsObj tmp=JsonConvert.DeserializeObject(jsonString);

废话!我想知道你的解决方案,但我对自己很强硬:不!请!!!这可能是一种简单的归档方法!!!!但是没关系!tyvm,Zaven!废话!我想知道你的解决方案,但我对自己很强硬:不!请!!!这可能是一种简单的归档方法!!!!但是没关系!tyvm,Zaven!