Parsing JSON seems like it is a pretty common topic of discussion on here. I have looked around and still have not found what I am looking for.
Here is my code for my HttpClient
public class CreateJsonRequest {
public static String SendJsonRequest(String URL, Map<String,Object> params){
try{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(URL);
JSONObject holder = new JSONObject();
for (Map.Entry<String, Object> m : params.entrySet()){
try {
holder.put(m.getKey(), m.getValue());
}
catch (JSONException e) {
Log.e("Hmmmm", "JSONException : "+e);
}
}
StringEntity se;
se = new StringEntity(holder.toString());
httpPost.setEntity(se);
httpPost.setHeader("Accept", "text/json");
httpPost.setHeader("Content-type", "text/json");
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
if(entity != null){
final JSONObject respObject = new JSONObject(EntityUtils.toString(entity));
String result = respObject.toString();
parseJSON(result);
I am using a HttpClient to send a JSON request to a server. The server then returns a response in JSON. This works great. Now here is where I am running into trouble.
I am receiving an HttpEntity from the server. I am then turning that into a string which looks like this. {"Make":"Ford","Year": 1975, "Model":"Mustang"}
I want to be able to send this string to my parseJSON(String jString)
method and it return a key value map. Where I feel this differs from other posts is that I want the parse method to be able to create a key value map for any JSON string I send it. So if I sent it {"Engine":"v8","Cylinders": 8, "Transmission":"Manual","Gears": 4}
it would still work. Is this doable? And if so, could you give me some nudges in the right direction?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…