简体   繁体   English

JSON字符串到JSON对象的转换

[英]Json string to json object conversion

I have looking to similar questions but i was not able to solve my problem .I have tried parsing my json response both into a Json array and Json object but every time , i am getting this exception of string to json object. 我一直在寻找类似的问题,但我无法解决问题。我尝试将json响应都解析为Json数组和Json对象,但是每次,我都会将此字符串的异常发送给json对象。 I guess may be the problem is with my Json response. 我想可能是我的Json响应出现了问题。

 results = { "type": 1, "user_log": "ahsan.tahir.92_/var/www/html/2014-10-11__15-54-52__50sec.txt_res", "freq": 0.01, "coordina": [ [-37], [-9], [-20], [-12], [-22], [-9], [-22], [-15], [-25], [-7], [-20], [-12], [-20], [-9], [-25], [8], [-23], [-11], [-18], [-13], [-19], [-10], [-21], [-12], [-25], [-11], [-17], [-12], [-22], [-13], [-21], [3] ], "tot_time": 40.11, "tot_distace": 100, "stroke_each_pool": [ [16], [16] ], "tot_stroke": [ [16], [16] ], "split": [ [20.22], [19.89] ], "timing_turn": 7.04, "cycle_Rate_l": [ [2.22], [2.2680001] ], "cycle_Rate_r": [ [2.224], [2.27] ], "mean_velocity": [ [2.4727993], [2.5138261] ], "stroke_length": [ [3.125], [3.125] ], "stroke_freq": [ [79.077431], [77.419357] ], "roll_peaks": [ [-44.10043335], [55.79428101], [-61.51541138], [54.7466507], [-62.09820557], [55.01488495], [-62.48770142], [53.44023132], [-70.32449341], [51.8399353], [-65.84837341], [53.5617981], [-63.50210571], [55.9821167], [-62.37905121], [39.42669678], [-43.44207764], [63.20912933], [-59.19660187], [50.6708374], [-63.8214798], [54.57595062], [-63.31864166], [53.82037354], [-66.93650818], [52.36277008], [-65.23461151], [52.89829254], [-62.78508759], [51.17367554], [-62.87123108], [59.13114929] ], "mean_roll_dx": [ [52.475822], [54.73027] ], "mean_roll_sx": [ [61.531971], [60.950779] ], "std_roll_dx": [ [5.4471908], [4.3127728] ], "std_roll_sx": [ [7.6123171], [7.4134283] ], "mean_roll": [ [57.003899], [57.840527] ], "std_roll": [ [7.9220791], [6.6817732] ], "mean_pitch": [ [-5.5227709], [-5.2282872] ], "std_pitch": [ [-5.5227709], [-5.2282872] ], "clean_stroke_time": [ [15.92], [16.84] ], "errore": 227, "fatal_error": { "_ArrayType_": "double", "_ArraySize_": [0,0], "_ArrayData_": null } } 

and i am parsing it like this : 我正在解析它是这样的:

 JSONObject reader = new JSONObject(in); JSONObject sys = reader.getJSONObject("results"); 

Any idea what am i missing here ? 知道我在这里想念什么吗?

My exception is as follows : 我的例外情况如下:

 org.json.JSONException: Value type of type java.lang.String cannot be converted to JSONObject 

There is no results field in the results string you have posted, is that the issue. 您发布的results字符串中没有results字段,就是问题所在。 In any case, please go ahead and reformat your question to more accurately reflect what you are doing, and include the error message you are seeing. 无论如何,请继续对您的问题进行重新格式化,以更准确地反映出您在做什么,并附上您看到的错误消息。

Actually, are you referring to the error message in the JSON output? 实际上,您是否在JSON输出中引用错误消息? That seems to be a JSON-encoded error message that was output by whatever service you called. 这似乎是由您调用的任何服务输出的JSON编码错误消息。 There seems to be some issue with the data you are passing to that service. 您传递给该服务的数据似乎有些问题。

I may have Misunderstood your question, but where are you parsing the Json response using something like JSONObject jsonObject = (JSONObject) jsonParser.parse(strippedJSON); 我可能误解了您的问题,但是您在哪里使用JSONObject之类的东西来解析Json响应jsonObject =(JSONObject)jsonParser.parse(strippedJSON); where strippedJSON the response received from API. 其中strippedJSON从API收到的响应。

This: 这个:

results = {rest of response here}

is not valid JSON. 是无效的JSON。

Perhaps you meant to return this: 也许您打算返回此:

{"results": {rest of response here}}

... or just ... 要不就

{rest of response here}

Your reader object is your results object itself. 您的reader对象是您的results对象本身。

JSONObject resultsJSON = new JSONObject(in);
System.out.println("User Log: " + resultsJSON.get("user_log"));

You're getting the exception because there's no results object in your input JSON string ie you're receiving your results object without a JSON wrapper around it. 之所以会出现异常,是因为您输入的JSON字符串中没有results对象,也就是说,您收到的results对象周围没有JSON包装器。

If you are using javascript try this 如果您使用的是javascript,请尝试以下操作

var parsed_data = JSON && JSON.parse(results) || var parsed_data = JSON && JSON.parse(结果)|| $.parseJSON(results); $ .parseJSON(结果);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM