0

I Create a one web API. its main aim is a request to another server and get the response from that server. I successfully get the response for a particular server.

I get the response(its a JSON Format) is below.

{
    "id": "test@gmail.com",
    "active": 1,
    "is_logged": true,
    "token": "hsja3t56yJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6InRlc3RAZHZlby5jb20iLCJwYXNzd29yZCI6InRlc3QyMDE4KyIsImV4cGlyZU9uIjoiMjAxOS0wNi0yMVQwNTozNzowOC4xODhaIn0.3wgGeL_HvcoEJJeEF7tj8jeXk2uIKpOoi9ewmK5yhteh",
    "status": "OK",
    "usertype": "TestUser",
    "msg": "Login Successfull."
}

I try to separate using split function

string[] sep = response.Split(',');

foreach (string any in sep)
    Console.WriteLine(any);

//string[] colon = sep[0].Split(':');
string[][] colon = sep.Select(x => x.Split(':')).ToArray();

//int count = colon.Count();
for (int i = 0; i <= colon.Length; i++)
{
     Console.WriteLine(colon[i][0]);
     Console.WriteLine(colon[i][1]);           
}

Any other way to separate the response? I also use all the field in some other purpose.

phuzi
  • 12,078
  • 3
  • 26
  • 50
  • If you know the JSON string that you are receiving then you can create a class and Deserialize to the class using `JsonConvert.DeserializeObject(Json string)`, if you don't know the keys, use `JsonConvert.DeserializeObject(json string)`. Also look at this similar question https://stackoverflow.com/questions/4749639/deserializing-json-to-net-object-using-newtonsoft-or-linq-to-json-maybe – Bosco Jun 19 '19 at 06:30
  • I return response use in `return JsonConvert.DeserializeObject(response);` – Nikunj Chaklasiya Jun 19 '19 at 06:35
  • What do you want to do with the response?. Save to the db or send to the View, or ? – Bosco Jun 19 '19 at 06:38
  • Its only view purpose. I don't use any database Now. – Nikunj Chaklasiya Jun 19 '19 at 06:40
  • So you want to read it with JavaScript in the view or JavaScript in Console application.?, When you say seperate, the JSON string is already separated and you can pass or convert it from string to a class object for any use – Bosco Jun 19 '19 at 06:42
  • my response like response : { "id": "test@gmail.com", "active": 1, "is_logged": true, "token": "hsja3t56yJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6InRlc3RAZHZlby5jb20iLCJwYXNzd29yZCI6InRlc3QyMDE4KyIsImV4cGlyZU9uIjoiMjAxOS0wNi0yMVQwNTozNzowOC4xODhaIn0.3wgGeL_HvcoEJJeEF7tj8jeXk2uIKpOoi9ewmK5yhteh", "status": "OK", "usertype": "TestUser", "msg": "Login Successfull." } this format – Nikunj Chaklasiya Jun 19 '19 at 06:45
  • But i access active = 1; id = "test@dveo.com"; "is_logged" = 1; msg = "Login Successfull."; status = OK; token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6InRlc3RAZHZlby5jb20iLCJwYXNzd29yZCI6InRlc3QyMDE4KyIsImV4cGlyZU9uIjoiMjAxOS0wNi0xOVQxMjoxODo0Ny40NTdaIn0.mrOxGPCjEdJQ_ngwN9OH4d0Ca-xIr-jGbIGsPkyXzCk"; usertype = TestUser; this format – Nikunj Chaklasiya Jun 19 '19 at 06:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/195171/discussion-between-bosco-and-nikunj-chaklasiya). – Bosco Jun 19 '19 at 06:46

3 Answers3

2

Create a Class based on your response property:

    public class UserData
    {
        public string id { get; set; }
        public int active { get; set; }
        public bool is_logged { get; set; }
        public string token { get; set; }
        public string status { get; set; }
        public string usertype { get; set; }
        public string msg { get; set; }
    }

On reading the response data, use JsonConvert.DeserializeObject

    string response = "{\"id\":\"test @gmail.com\",\"active\":1,\"is_logged\":true,\"token\":\"hsja3t56yJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6InRlc3RAZHZlby5jb20iLCJwYXNzd29yZCI6InRlc3QyMDE4KyIsImV4cGlyZU9uIjoiMjAxOS0wNi0yMVQwNTozNzowOC4xODhaIn0.3wgGeL_HvcoEJJeEF7tj8jeXk2uIKpOoi9ewmK5yhteh\",\"status\":\"OK\",\"usertype\":\"TestUser\",\"msg\":\"Login Successfull.\"}";
    var responseData = JsonConvert.DeserializeObject<UserData>(response);

        //here the print in JSON Data

        Console.WriteLine("id : " + responseData.id);
        Console.WriteLine("active : " + responseData.active);
        Console.WriteLine("is_logged : " + responseData.is_logged);
        Console.WriteLine("token : " + responseData.token);
        Console.WriteLine("status : " + responseData.status);
        Console.WriteLine("usertype : " + responseData.usertype);
        Console.WriteLine("msg : " + responseData.msg);
Skaria Thomas
  • 419
  • 3
  • 10
  • my response is not static it gave from another server that's created on Node.JS. – Nikunj Chaklasiya Jun 19 '19 at 06:54
  • Assign the dynamic json data to response variable – Skaria Thomas Jun 19 '19 at 06:56
  • I use the same as your answer `return JsonConvert.DeserializeObject(response);` – Nikunj Chaklasiya Jun 19 '19 at 06:58
  • I give successfully print the data in console like { "id": "test@gmail.com", "active": 1, "is_logged": true, "token": "hsja3t56yJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6InRlc3RAZHZlby5jb20iLCJwYXNzd29yZCI6InRlc3QyMDE4KyIsImV4cGlyZU9uIjoiMjAxOS0wNi0yMVQwNTozNzowOC4xODhaIn0.3wgGeL_HvcoEJJeEF7tj8jeXk2uIKpOoi9ewmK5yhteh", "status": "OK", "usertype": "TestUser", "msg": "Login Successfull." } format – Nikunj Chaklasiya Jun 19 '19 at 06:59
  • But I display here all are display in individual like active = 1; id = "test@dveo.com"; "is_logged" = 1; msg = "Login Successfull."; status = OK; token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6InRlc3RAZHZlby5jb20iLCJwYXNzd29yZCI6InRlc3QyMDE4KyIsImV4cGlyZU9uIjoiMjAxOS0wNi0xOVQxMjoxODo0Ny40NTdaIn0.mrOxGPCjEdJQ_ngwN9OH4d0Ca-xIr-jGbIGsPkyXzCk"; usertype = TestUser; it is possible – Nikunj Chaklasiya Jun 19 '19 at 07:00
0

use Newtonsoft.json.dll by adding the NuGet package, Then convert the response to json object

JObject jo = JObject.Parse(searchCondition);

foreach (JToken child in jo.Children()) {
    var prop = child as JProperty;
    if (prop.Value != null && !string.IsNullOrEmpty(prop.Value.ToString())) {
        string name=prop.Name;
        string value = prop.Value;
        //You can now do whatever with the values like put in a list of object
    }
}
Bosco
  • 1,536
  • 2
  • 13
  • 25
Zhenghao
  • 14
  • 2
0

This is My own example to get the properties from JSON string, you can use this.

But first, you need to install this package:-> Newtonsoft.Json.Linq to access JObject

using System;
using Newtonsoft.Json.Linq;

public class Program
{
     public static void Main()
     {      
           string jsonString = "{\"firstname\":\"Alex Wu\",\"lastname\":\"type\"}";
           JObject jObject = JObject.Parse(jsonString); 
           string firstname = (string)jObject.SelectToken("firstname");
           string lastname = (string)
           Console.WriteLine("{0}", firstname);
           Console.ReadLine();
     }
}
Axel Blaz
  • 29
  • 7