简体   繁体   中英

how to parse the value of json result in c#?

I am coming to a problem where I am displaying {"name": "Mike"} as my result rather then the value of it which is Mike. Is there a way to parse or get the value of json result to display properly with my code below without use Newton or any c# libraries? Thanks for the help.

here is my code:

     public void CheckUserInDb()
    {

        var httpWebRequest = (HttpWebRequest) WebRequest.Create("https://PROJECT_URL.firebaseio.com/QR.json");
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "PUT";

       using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        { 


          string missingObjectCount = statusText.text;
           streamWriter.Write(missingObjectCount);



        }

        var httpResponse = (HttpWebResponse) httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();

            Debug.Log(result);
        }

        SceneManager.LoadScene("Verify");


    }


} 

Well, sure. After all, JSON is just string data.

So if result is just a string like {"name": "Mike"} , then simply parse out the value you want. There are a few ways to do it. For example:

string result = "{\"name\": \"Mike\"}";
string[] parsed = result.Split('"');
Console.WriteLine(parsed[3]);

This is not quite as simple if the JSON data is not always predictable, ie the number of fields, the field positions, etc.

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