GVKun编程网logo

转换jsonArray异常——由hibernate引起的转换jsonArray异常解决办法(jsonarray转换为jsonobject)

14

如果您对转换jsonArray异常——由hibernate引起的转换jsonArray异常解决办法感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于转换jsonArray异常—

如果您对转换jsonArray异常——由hibernate引起的转换jsonArray异常解决办法感兴趣,那么本文将是一篇不错的选择,我们将为您详在本文中,您将会了解到关于转换jsonArray异常——由hibernate引起的转换jsonArray异常解决办法的详细内容,我们还将为您解答jsonarray转换为jsonobject的相关问题,并且为您提供关于AJAX向SpringMVC controller 传JSONArray并将String转换成JSONArray、Android ArrayAdapter和JSONArray、android – org.json.jsonarray无法转换为jsonobject错误、delphi的TJSONArray出错:undeclare identify 'TJSONArray'...的有价值信息。

本文目录一览:

转换jsonArray异常——由hibernate引起的转换jsonArray异常解决办法(jsonarray转换为jsonobject)

转换jsonArray异常——由hibernate引起的转换jsonArray异常解决办法(jsonarray转换为jsonobject)

//如下设置避免hibernate关联关系引起的转换jsonArray异常
JsonConfig jsonConfig = new JsonConfig(); //建立配置文件
jsonConfig.setIgnoreDefaultExcludes(false); //设置默认忽略
jsonConfig.setExcludes(new String[]{"ewmInfo"});
JSONArray jsonArray = JSONArray.fromObject(list,jsonConfig);
toWrite(jsonBuilder.returnSuccessjson("'"+jsonArray+"'"));

—— 这样,就可以了

AJAX向SpringMVC controller 传JSONArray并将String转换成JSONArray

AJAX向SpringMVC controller 传JSONArray并将String转换成JSONArray

http://stackoverflow.com/questions/14515785/how-to-convert-json-string-arrray-to-json-array-list


create JSONArray ([{},{},{}]) in JS

var data = [];
data.push({
		"id" : 1,"val" : $('#projectName').val()
	});
	data.push({
		"id" : 2,"val" : $('#description').val()
	});
	data.push({
		"id" : 3,"val" : $('#startdate').val()
	});
	data.push({
		"id" : 4,"val" : STATUS_NOT_START
	});
	var id = 5;
	while($("#" + id).length > 0){
		data.push({
			"id" : id,"val" : $("#" + id).val()
		});
		id++;
	}

ajax pass JSONArray String to Controller:
$.ajax({
		type : "Post",url : "createProject.html",data : "jsonArray=" + JSON.stringify(data) + "&depId=" + depId + "&groId=" + groId + "&objId=" + objId,success : function(response){
			var alertText = "Project " + $('#projectName').val() + " is successfully created! Project ID: " + response;
			adDalert("alert-success",alertText,"#alertdiv");
		},error : function(e){
			var alertText = 'Error: ' + e;
     		adDalert("alert-error","#alertdiv");
		}
	});

Controller将JSONArray String转换成JSONArray
import net.sf.json.JSONArray; JSONException; JSONObject; JSONSerializer; public class TestJson { static void parseProfilesJson(String the_json){ try { JSONArray nameArray =(JSONArray) JSONSerializer.toJSON(the_json); System.out.println(nameArray.size()); for(Object js : nameArray){ JSONObject json = (JSONObject)js; System.out.println(json.get("date")); } } catch (JSONException e) { e.printstacktrace(); } } void main(String[] args) { String s = "[{\"date\":\"2012-04-23\",\"activity\":\"gym\"},{\"date\":\"2012-04-24\",\"activity\":\"walking\"}]"; parseProfilesJson(s); } }

Android ArrayAdapter和JSONArray

Android ArrayAdapter和JSONArray

我是Android开发的新手.

考虑到JSON Carrier与XML相比的轻巧性,我纯粹喜欢使用JSON Objects和Arrays作为我的简单应用程序.

我对ArrayAdapter提出了挑战,要求填充ListView.

这就是我如何克服并需要你的建议.

Extend the Adaptor class.

然后将JSONArray传递给构造函数.
这里构造函数使用设置JSONArray长度的虚拟String数组调用super.
将构造函数参数存储在类中以供进一步使用.

public myAdaptor(Context context, int resource, JSONArray array)
{
    super(context, resource, new String[array.length()]);
    // Store in the local varialbles to the adapter class.
    this.context = context;
    this.resource = resource;
    this.profiles = objects;
}

getView()将完成从JSONArray获取JSONObjects以构建视图的工作.

public View getView(int position, View convertView, ViewGroup parent)
{
    View view;
    if (convertView == null)
    {
        LayoutInflater inflater = (LayoutInflater) 
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(resource, parent, false);
    }
    else
    {
        view = convertView;
    }

    // Here 
    JSONObject item = (JSONObject) profiles.getJSONObject(position);

    // READY WITH JSONObject from the JSONArray
    // YOUR CODE TO BUILD VIEW OR ACCESS THE 
}

现在任何改进/建议/尽管问题?

解决方法:

我建议你使用谷歌GSON而不是JSON.它是一个从JSON请求中为您提供创建对象的库,您不再需要解析JSON.只需创建一个对象,其中包含JSON请求中的所有字段,并且命名相同,并且无论您想要什么,都可以使用它 – 例如:

Your JSON request
{
    [
        {
            "id": "2663",
            "title":"qwe"

        },
        {
            "id": "1234",
            "title":"asd"
        },
        {
            "id": "5678",
            "title":"zxc"
        }

    ]
}

你的类 – JSON-Array的项目

 public class MyArrayAdapterItem{
     int id;
     String title;
 }

您下载数据的代码中的Somwhere.我不知道你是怎么做的所以我会发布我的代码例如:

mGparser = new JsonParser();
Gson mGson = new Gson();

Url url = "http://your_api.com"
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Connection", "close");
conn.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

JsonArray request = (JsonArray) mGparser.parse(in.readLine());
in.close();
ArrayList<MyArrayAdapterItem> items = mGson.fromJson(request, new Typetoken<ArrayList<MyArrayAdapterItem>>() {}.getType());

所以,现在只需将“items”替换为适配器构造函数中的JSON数组

android – org.json.jsonarray无法转换为jsonobject错误

android – org.json.jsonarray无法转换为jsonobject错误

我一直试图修复此错误2天,搜索并尝试从stackoverflow.com多种编码类型.我已经检查了我的 JSON http://jsonformatter.curiousconcept.com/#jsonformatter.但我仍然无法找出我的代码为什么这样做.我有3个文件. MainACtivity.java应该从我的服务器上的test.json文件中获取信息,然后将其处理到Events.java. Events.java只显示信息,但应用程序没有那么远.

在任何人的情况下更新的代码都需要这个的固定.

我的错误:

01-14 22:18:08.165: E/JSON Response:(419): > { "event":[ 
    01-14 22:18:08.165: E/JSON Response:(419):      {
    01-14 22:18:08.165: E/JSON Response:(419):       "event_name":"Test Event",01-14 22:18:08.165: E/JSON Response:(419):       "event_time":"7:00pm",01-14 22:18:08.165: E/JSON Response:(419):       "event_price":"$15.00"
    01-14 22:18:08.165: E/JSON Response:(419):      }
    01-14 22:18:08.165: E/JSON Response:(419):   ] 
    01-14 22:18:08.165: E/JSON Response:(419): }
    01-14 22:18:08.175: E/Json Error(419): Error: org.json.JSONException: Value         [{"event_price":"$15.00","event_time":"7:00pm","event_name":"Test Event"}] at event of type     org.json.JSONArray cannot be converted to JSONObject

MainActivity.java

package com.example.dba;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity 
{

String event_name,event_time,event_price;
static JSONObject object =null;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new PrefetchData().execute();
}

/**
 * Async Task to make http call
 */
private class PrefetchData extends AsyncTask<Void,Void,Void> 
{

    @Override
    protected void onPreExecute() 
    {
        super.onPreExecute();      
    }

    @Override
    protected Void doInBackground(Void... arg0) 
    {

        JsonParser jsonParser = new JsonParser();
        String json = jsonParser.getJSONFromUrl("http://www.website/test.json");

        Log.e("JSON Response: ","> " + json);

        if (json != null) 
        {
           try 
            {
                JSONObject parent = new JSONObject(json);
                JSONArray eventDetails = parent.getJSONArray("event");

                for(int i=0; i < eventDetails.length(); i++)
                {
                    object = eventDetails.getJSONObject(i);
                    event_name = object.getString("event_name");
                    event_time = object.getString("event_time");
                    event_price = object.getString("event_price");

                    Log.e("JSON","> " + event_name + event_time + event_price );
                }
            }  catch (JSONException e) 
                {
                Log.e("Json Error","Error: " + e.toString());
                    e.printstacktrace();
                }

        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) 
    {           
        super.onPostExecute(result);

        Intent i = new Intent(MainActivity.this,Events.class);
        i.putExtra("event_name",event_name);
        i.putExtra("event_time",event_time);
        i.putExtra("event_price",event_price);
        startActivity(i);

        // close this activity
        finish();
    }

}

}


}

解决方法

要更改的相关代码 – 在MainActivity.java的doInBackground()中:

JSONObject eventDetails = parent.getJSONObject("event");

至:

JSONArray eventDetails = parent.getJSONArray("event");

delphi的TJSONArray出错:undeclare identify 'TJSONArray'...

delphi的TJSONArray出错:undeclare identify 'TJSONArray'...

总结

以上是小编为你收集整理的delphi的TJSONArray出错:undeclare identify ''TJSONArray''...全部内容。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。

今天的关于转换jsonArray异常——由hibernate引起的转换jsonArray异常解决办法jsonarray转换为jsonobject的分享已经结束,谢谢您的关注,如果想了解更多关于AJAX向SpringMVC controller 传JSONArray并将String转换成JSONArray、Android ArrayAdapter和JSONArray、android – org.json.jsonarray无法转换为jsonobject错误、delphi的TJSONArray出错:undeclare identify 'TJSONArray'...的相关知识,请在本站进行查询。

本文标签: