简体   繁体   中英

convert json string into array or object

I get some json data form the web which is like:

[{
  "pk": 1,
  "model": "stock.item",
  "fields": {
    "style": "f/s",
    "name": "shirt",
    "color": "red",
    "sync": 1,
    "fabric_code": "0012",
    "item_code": "001",
    "size": "34"
  }
}, {
  "pk": 2,
  "model": "stock.item",
  "fields": {
    "style": "febric",
    "name": "Trouser",
    "color": "red",
    "sync": 1,
    "fabric_code": "fabric code",
    "item_code": "0123",
    "size": "44"
  }
}]

How can i use it in the C# winforms desktop application. I already get this data in the form of string.
Edit:
I also try to use this way..

JavaScriptSerializer ss = new JavaScriptSerializer();
object itm = ss.DeserializeObject(responseFromServer);

It returns 'System.Object[]' but i also dont know that how i can use this.
All types of answer are welcome.

我认为你追求的是JSON.NET

The documentation for JavaScriptSerializer specifies that for your purpose you should use Json.NET instead.

However, if you prefer to continue to use JavaScriptSerializer you need to do the following:

JavaScriptSerializer ss = new JavaScriptSerializer();
var itm = ss.DeserializeObject(responseFromServer);

The StackOverflow thread difference-between-var-and-object-in-c-sharp describes how using var implicitly types the variable, so that it's easier to work with, where-as your code uses object , which strongly types it to an object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM