3

How to fetch "full_name_ac" in the following JSON :-

{
    "rows": 10,
    "os": "0",
    "page": "1",
    "total": "1",
    "peoples": {
        **"123":** {
            "url": "http://google.com",
            **"id": "123",**
            "fname": "Rob",
            "lname": "Tom",
            "full_name_ac": "Rob Tom"
        }
    }
}

I can not create model because model is always going to be changed according to "id".

I am trying something like that but not sure how to get the value of full_name_ac

JObject obj = JObject.Parse(data);
zastrowm
  • 8,017
  • 3
  • 43
  • 63
F11
  • 3,703
  • 12
  • 49
  • 83
  • See [Searching for a specific JToken by name in a JObject hierarchy](http://stackoverflow.com/a/19646950/3956290) – Mehrzad Chehraz Mar 28 '16 at 07:18
  • Are you alright with just getting the value, or do you want to create a class/model and deserialize into that object? – zastrowm Mar 28 '16 at 07:27
  • Mackie I want to get the value of full name and your answer is spot on..will mark it as an answer – F11 Mar 28 '16 at 07:28

6 Answers6

3

I'd recommend you take a look at using JSONPath(s) and use SelectTokens:

JObject obj = JObject.Parse(data);
var names = obj.SelectTokens("$.peoples.*.full_name_ac");

var allNamesAsCsv = String.Join(",", names.Values<string>()); 

Of course, if you always know that there will always be just one, you can use SelectToken:

var onlyMatchObject = obj.SelectToken("$.peoples.*.full_name_ac");
var onlyMatch = first.Value<string>();
zastrowm
  • 8,017
  • 3
  • 43
  • 63
2

Use Json.Net. try to use dyanamic

dynamic stuff = JsonConvert.DeserializeObject(YOUR-JSON_STRING);

string name = stuff.peoples.123.full_name_ac;

See this link for more info:Deserialize JSON into C# dynamic object?

model is always going to be changed according to "id".

If your model is always changes then you have create one model which contains id and string. String object is a json string of fields. So that you can check Id and it's model fields. so you can compare that fields with json.

"field" :
{
    "id" : 123
    "fields" : 
    {
        "fname":"string",
        "full_name_ac":"string"
    }
}

Create json something like above and include this json in your json. When you deserialize your main json you can compare fields. I thing from above you will get some basic idea.

If your model is dynamic then there is only one option i.e. you have to create a json something like above which contains fields. So that you can compare that fields with your actual json value.

Community
  • 1
  • 1
Ajay
  • 6,418
  • 18
  • 79
  • 130
1

Maybe you can use a Regex and some basic text parsing, to identify the "full_name_ac" property, and subtract the value, something like:

// just an example, untested
string jsonText = "{...}";
int startIndex = jsonText.indexOf(@"""full_name_ac"":");
int stopIndex = jsonText.indexOf(startIndex, "}");
string value = jsonText.substring(startIndex, stopIndex);
Catalin
  • 11,503
  • 19
  • 74
  • 147
  • If possible, it seems like it'd be better to avoid regex. What happens if it's no longer followed by a '}' but another value comes after it? – zastrowm Mar 28 '16 at 07:35
  • Its not the most elegant solution, but the solution was good if the model was completely dynamic (which seems not to be the case). Anyways, you either get a comma `,` or a curly brace, so its still possible, but requires some tests – Catalin Mar 28 '16 at 08:09
0

Fetch the value of required token of the deserialized object (in your case obj.peoples has first token as "123" and first token of "123" is the object which has the required properties) and get the required property i.e. full_name_ac from it.

        dynamic obj = JObject.Parse(jsonText);
        var value = obj.peoples;

        var tokenPeople = ((Newtonsoft.Json.Linq.JContainer)obj.peoples).First.First;
        string peopleJson =tokenPeople.ToString();
        dynamic people = JObject.Parse(peopleJson);
        string full_name_ac = people.full_name_ac;
M.S.
  • 4,283
  • 1
  • 19
  • 42
0

using JObject.Parse(jsonString) AND get First element

// Id is dynamic , so parse and get first element
string dynamicName = (string)JObject.Parse(data)["peoples"].Children().First().Children().First()["full_name_ac"];
Andi AR
  • 2,678
  • 2
  • 23
  • 28
0

Following line will help you to get the value of full_name_ac

var full_name_ac = obj.SelectToken("peoples.123.full_name_ac").ToString();
Venu prasad H S
  • 231
  • 1
  • 8