且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何读取json数据并将其转换为c#中的x#

更新时间:2022-12-12 08:44:00

添加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;