Click here to Skip to main content
14,865,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am working on calling an api GET method and save the json payload as a json document and save to a drive. I can call the api but not sure how to save the payload results as a json document. I would like to save the results as json document to a drive C:\jsonDocs\weather.json

api GET url: http://myapi.com/v1/weatheradata?id=1[^]

json payload:

{4 items
"message":"accurate"
"cod":"200"
"count":1
"list":[1 item
0:{11 items
"id":2643743
"name":"London"
"coord":{2 items
"lat":51.5085
"lon":-0.1257
}
"main":{6 items
"temp":291.57
"feels_like":288.33
"temp_min":290.37
"temp_max":292.59
"pressure":1024
"humidity":42
}
"dt":1595275779
"wind":{2 items
"speed":3.1
"deg":10
}
"sys":{1 item
"country":"GB"
}
"rain":NULL
"snow":NULL
"clouds":{1 item
"all":7
}
"weather":[1 item
0:{4 items
"id":800
"main":"Clear"
"description":"clear sky"
"icon":"01n"
}
]
}
]
}


What I have tried:

I am using the below code.

string json = string.Empty;
string cities;
cities = getCities();

JavaScriptSerializer oJS = new JavaScriptSerializer
{
    MaxJsonLength = Int32.MaxValue
};

json = oJS.Serialize(cities.ToList());
using (StreamWriter sw = new StreamWriter(File.Create(@"C:\temp\cities.json")))
{
    sw.Write(json);
}


This generates no error but the json document is blank.
Posted
Updated 23-Jul-20 9:47am
v6
Comments
F-ES Sitecore 22-Jul-20 7:16am
   
Google how to get the response from HttpWebResponse, then google how to save a stream to a text file. These things are all very well documented.

Use NewtonSoft and serialize to a file. See Serialize JSON to a file[^]
   
Comments
Member 12586110 21-Jul-20 16:34pm
   
Hi Mike Hankey, Thanks very much for your response. Could we accomplish the same using microsoft libraries.

Regards
Mike Hankey 21-Jul-20 16:41pm
   
NewtonSoft is a very stable utility and is very popular. You can use NuGet package manager to load it into your application. As far as uSoft JSON utilities, I don't use them because NewtonSoft is so much better. I don't know if uSoft has similar functionality.
Member 12586110 21-Jul-20 19:07pm
   
Sure. Thank you.
The data you have is already close to being a proper JSON file, but you need to remove all the "x item(s)" strings:
JavaScript
{4 items // these strings need to be removed
    "message":"accurate"
    "cod":"200"
    "count":1
    "list":[1 item
        0:{11 items
            "id":2643743
            "name":"London"
            "coord":{2 items
                "lat":51.5085
                "lon":-0.1257
            }
            "main":{6 items
                "temp":291.57
                "feels_like":288.33
                "temp_min":290.37
                "temp_max":292.59
                "pressure":1024
                "humidity":42
            }
            "dt":1595275779
            "wind":{2 items
                "speed":3.1
                "deg":10
            }
            "sys":{1 item
                "country":"GB"
            }
            "rain":NULL
            "snow":NULL
            "clouds":{1 item
                "all":7
            }
            "weather":[1 item
                0:{4 items
                    "id":800
                    "main":"Clear"
                    "description":"clear sky"
                    "icon":"01n"
                }
            ]
        }
    ]
}

I have added indentations so you can see the different parts of the data. But since this is already in JSON format you just need to use a TextWriter to write it to the file. What you do with it after that is not clear.
   
Comments
Member 12586110 21-Jul-20 19:10pm
   
Hi Richard MacCutchan, Thank you for your response. I would like to save the json document to a drive for instance "C:\jsonDocs\weather.json"
Richard MacCutchan 22-Jul-20 3:31am
   
So what is the problem in writing a lot of lines of text to a file?
Member 12586110 22-Jul-20 16:59pm
   
Hi Richard, Modified my question accordingly, I am using JavaScriptSerializer. The api payload is json. I am deserializing and serializing is there a slick way to accomplish this.

City[] Cities = oJS.Deserialize<city[]>(cities);

json = oJS.Serialize(Cities);
Richard MacCutchan 23-Jul-20 4:08am
   
Why are you deserializing it just so you can serialize it again? You have the raw JSON text from your RESTful service, so you can just write that out to your textfile straight away.
Member 12586110 23-Jul-20 10:09am
   
Hi Richard, I tried but couldn't get it to write the raw file.
Richard MacCutchan 23-Jul-20 12:16pm
   
Tried what? Please use the Improve question link above and show us the code you have tried, and explain what happened.
Member 12586110 23-Jul-20 13:28pm
   
Hi Richard, Made changes to the code. I tried the below as well no luck.

List<object> buildingslist = new List<object>();

json = oJS.Serialize(buildingslist);

Thank you.
Richard MacCutchan 23-Jul-20 15:11pm
   
So what happened? You cannot expect us to guess what you mean by "no luck". Please think about what your code is trying to do, and explain it in proper detail, including what results you expect, what results you see, and any error messages or exceptions.
Member 12586110 23-Jul-20 15:52pm
   
Hi Richard, The json document is blank no error. If switch the code back to the deserialize and serialize that works.

City[] Cities = oJS.Deserialize<city[]>(cities);
json = oJS.Serialize(Cities);
Richard MacCutchan 24-Jul-20 3:36am
   
Well I guess you did not read my previous reply.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900