如何读取json数据并将其转换为c#中的x# [英] how to read json data and convert it to xml in c#

查看:77
本文介绍了如何读取json数据并将其转换为c#中的x#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我还是Json的初学者,我想知道如何读取包含如下所示的json数据的txt文件,以及如何将其转换为xml数据并提供它。



这里是我的json数据,例如:

Hello , I am still beginner to Json, I want to know how to read a txt file contain json data like this below and how to convert it to xml data an present it.

here is my json data for example:

{
    "glossary": {
        "title": "example glossary",
		"GlossDiv": {
            "title": "S",
			"GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
					"SortAs": "SGML",
					"GlossTerm": "Standard Generalized Markup Language",
					"Acronym": "SGML",
					"Abbrev": "ISO 8879:1986",
					"GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
						"GlossSeeAlso": ["GML", "XML"]
                    },
					"GlossSee": "markup"
                }
            }
        }
    }
}



我在互联网上发现很多文章都在谈论这个但是没有人和我一起工作,例如我使用了一个名为JsonReaderWriterFactory的类和许多其他方法和类但我我很困惑,不知道该用什么。


I found many articles in the internet talking about that but no one of them worked with me like for example I an use a class called JsonReaderWriterFactory and many other methods and classes but I'm so confused don't know what to use .

推荐答案

添加Json.NET库(使用NuGet)你的项目并用它来转换 - 可以是两个方向...

打开 help [ ^ ]图书馆页面,在左侧窗格中选择Samples-> Converting XML->将JSON转换为XML ...

您将在那里找到工作样本...

主页图书馆: http://james.newtonking.com/json [ ^ ]
Add Json.NET library (use NuGet) to your project and use it for converting - can be both direction...
Open help[^] page of the library, on the left pane select Samples->Converting XML->Convert JSON to XML...
You will have a working sample there...
Home page of the library: http://james.newtonking.com/json[^]


在一天之后尝试做我正在寻找的东西我终于完成并感谢KORNfel Eliyahu peter

这里是解决方案:



FISRT:

如果我有一个json字符串我想读它:



我的字符串:



after one day trying to do what I was searching for finally I finished and thanks to "KORNfel Eliyahu peter"
here is the solution :

FISRT:
if I have a json string and I want to read it :

my string:

string json = "{\"employees\": [{ \"firstName\":\"John\" , \"lastName\":\"Doe\" },{ \"firstName\":\"Anna\" , \"lastName\":\"Smith\" }]}";

I do parsing using json.net library 

 JObject o = JObject.Parse(json);

here if print it 

 Console.WriteLine(o["employees"]);





SECOND:



从json字符串转换为XML:



MY JSON STRING:



SECOND:

Converting from json string to XML:

MY JSON STRING :

string json = @"{
   '?xml': {
     '@version': '1.0',
    '@standalone': 'no'
   },
   'root': {
     'person': [
       {
         '@id': '1',
        'name': 'Alan',
        'url': 'http://www.google.com'
      },
      {
'@id': '2',        'name': 'Louis',
        'url': 'http://www.yahoo.com'
     }    ]
  }}";





我使用XNODE来自



I use XNODE from

System.Xml.Linq





XNode node = JsonConvert.DeserializeXNode(json) ,根 );



在这里我转换了json字符串



最后我打印出来了:

Console.WriteLine(node.ToString());



不要忘记将其更改为string,因为屏幕上出现的任何内容都应该是字符串。



HERE是我在顶部使用的库和命名空间:



首先我下载了​​json.et库来自:点击你的装配>



XNode node = JsonConvert.DeserializeXNode(json,"root");

and here I converted the json string

finally I printed it :
Console.WriteLine(node.ToString());

don't forget to change it to "string" because anything appear on screen should be string.

HERE is the library and namespaces I used at top :

first I downloaded json.et library from : click on your assembly >

.select 'Manage Nuget Packages for Solution'
.click 'online', in the search box type 'newtonsoft'
.click 'Install' on 'Json.NET' (probably, what your looking for might be in another package, I'll ask you to track it down).







然后输入这些名称空间:




and then type these namespaces up :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.JsonResult;
using System.Xml.Linq;


这篇关于如何读取json数据并将其转换为c#中的x#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆