0

I know this question has already many answers on SO and luckily I have tried them but still unable to get my string parsed. I am getting the following JSON List from my api:

JSON string

The JSON text is:

[
 {"S1":"1","S2":"202010010","S30":"COCA COLA BEVERAGES PAKISTAN LTD. GUJRANWALA","S31":"Coca cola 1","D1":"2015-07-01T00:00:00","S5":"001","S32":"Local","S6":"T1","S33":"By Road","S10":""},
 {"S1":"3","S2":"202010010","S30":"COCA COLA BEVERAGES PAKISTAN LTD. GUJRANWALA","S31":"Coca cola 1","D1":"2015-07-01T00:00:00","S5":"002","S32":"Innter City","S6":"T1","S33":"By Road","S10":""},
 {"S1":"4","S2":"202010010","S30":"COCA COLA BEVERAGES PAKISTAN LTD. GUJRANWALA","S31":"Coca cola 1","D1":"2015-07-01T00:00:00","S5":"003","S32":"International","S6":"T2","S33":"By Sea","S10":""}
]

S1 will always be unique across the string. I want to get all S1 and show them in a spinner. And later on selection of one of S1 by user, I want to get the related data of that S1.

I have tried the answer to this question but when I tried to parse it gave me the following error:

Error reading JObject from Json reader. Current Json reader is not an object

Then I tried the following:

var jobno = JsonConvert.DeserializeObject<List<string>>(json.ToString());

as per the most voted answer to this question and got the following error:

Error reading string, Path '[0]', Line 1, Position 2.

I also tried to do something as explained in another SO question as follows:

var d = l.SelectMany (obj => obj.Select (inner => inner ["S1"].Value<string> ())).ToList ();

But it is a compile time error as:

Cannot apply indexing with [ ] to a expression of type char

How can I do that? and how can I query my JSON list later on the basis of S1?

I am generating this JSON from api as:

var result = db.Database.SqlQuery<JobNoQ>(query).ToList();
return Json(result);

I think that problem is with the JSON. May the square brackets in start and end of JSON are creating problems

Community
  • 1
  • 1
Awais Mahmood
  • 1,308
  • 4
  • 21
  • 51
  • Can you edit your question to include the text of the JSON that causes the problem? With just an image capture from a GUI viewer it's hard to see what is happening or to test fixes. – dbc Nov 21 '15 at 06:40

2 Answers2

1

Your root JSON container is an array of objects, not an single object. According to the standard:

  • An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).
  • An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).

Inside your array, each object consists of key/value pairs whose values are all strings. Thus the first linked answer is not relevant because it only applies when the root container is an object. And your second solution does not work because the array items are objects not strings.

Instead, the simplest way to deserialize your JSON is as a List<Dictionary<string, string>>:

var items = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(json);
var ids = items.Select(d => d["S1"]).ToList();

Then, later, when the user has selected an id, you can do:

var item = items.Select(d => d["S1"] == id).Single();

Or, if you have to do lots of lookups, you can build a dictionary first:

var lookup = items.ToDictionary(d => d["S1"], d => d);

And then later do

var item = lookup[id];
dbc
  • 104,963
  • 20
  • 228
  • 340
0

Take your JSON and use some third party soft like json2csharp.com get created classes and copy it to your project... then you could use DeserializeObject from Json.net.

Then you will have an entity which represents your JSON and you could map it easy with automapper or other system that you want.

SergioZgz
  • 148
  • 5